q394267077 于 2014.09.20 13:31 提问
- 百度地图获取经纬度没问题但是具体地址省城市街道有时能得到有时却是返回null求解救
-
public class PhotoFragment extends Fragment {
public static final String IMAGE_UNSPECIFIED = "image/*"; private ImageView iv_head = null; private String[] strings = null; private FileOutputStream b = null; private String mImagePath = null; private File sdcardTempFile = null; private Bitmap myBitmap = null; private byte[] mContent = null; private Button btn_geog = null; // 定位相关 private String loc = null; // 保存定位信息 private LocationClient mLocationClient = null; private BDLocationListener myListener = new MyLocationListener(); private LocationMode mCurrentMode; boolean isFirstLoc = true;// 是否首次定位 private String head_str = null; private Map<String, String> upMap = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_photo, container, false); initview(); btn_geog = (Button) view.findViewById(R.id.geog); iv_head = (ImageView) view.findViewById(R.id.head); // 用于保存上传的信息时使用 upMap = new HashMap<String, String>(); sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg"); iv_head.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity()); dialog.setTitle("请选择"); strings = new String[2]; strings[0] = "相机"; strings[1] = "相册"; dialog.setItems(strings, new DialogInterface.OnClickListener() { // which 如果是点击列表中的选项 表示当前点击的item的下标 @Override public void onClick(DialogInterface dialog, int which) { if (which == 0) { String status = Environment .getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); try { sdcardTempFile.createNewFile(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(sdcardTempFile)); } catch (IOException e) { e.printStackTrace(); } startActivityForResult(intent, 100); } else { Toast.makeText(getActivity(), "请安装储存卡", Toast.LENGTH_SHORT).show(); } } else if (which == 1) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); try { sdcardTempFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } startActivityForResult(intent, 101); } } }); dialog.create(); dialog.show(); } }); btn_geog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mLocationClient.start();// 开启定位SDK mLocationClient.requestLocation();// 开始请求位置 } }); return view; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 100 || data != null) { mImagePath = sdcardTempFile.getAbsolutePath(); File file = new File(mImagePath); // 这是需要上传的转换成Base64的String 他大爷的就是这里出错了 // head_str = bitmapToString(mImagePath); myBitmap = decodeFile(file); if (myBitmap != null) { iv_head.setImageBitmap(myBitmap); } } if (requestCode == 101 || null != data) { try { // ContentResolver是可以对ContentProvider操作的类 ContentResolver resolver = getActivity().getContentResolver(); Uri originalUri = data.getData();// 取数据 // 这是把输入流转成了字节数组Uri.parse需要一个路径openInputStream需要一个URI mContent = readStream(resolver.openInputStream(Uri .parse(originalUri.toString()))); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; opt.inDither = false; opt.inPurgeable = true; opt.inSampleSize = calculateInSampleSize(opt, 480, 800); // opt.inTempStorage = new byte[12 * 1024]; opt.inJustDecodeBounds = false; // 将字节数组转换为ImageView可调用的Bitmap对象 myBitmap = getPicFromBytes(mContent, opt); iv_head.setImageBitmap(myBitmap); mImagePath = getPath(originalUri); } catch (Exception e) { System.out.print(e.getMessage()); } } } /* * 压缩图片,避免内存不足报错 */ private Bitmap decodeFile(File f) { Bitmap b = null; try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); BitmapFactory.Options o2 = new BitmapFactory.Options(); int width = o.outWidth; int height = o.outHeight; int test = calculateInSampleSize(o, 480, 800); Log.i("照片长宽:", " width:" + width + " height:" + height + "insamplesize:" + test); o2.inSampleSize = calculateInSampleSize(o, 480, 800); fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { e.printStackTrace(); } return b; } // 计算图片的缩放值 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } public static byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) { if (bytes != null) if (opts != null) return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts); else return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); return null; } private String getPath(Uri originalUri) { String[] imgs = { MediaStore.Images.Media.DATA };// 将图片URI转换成存储路径 Cursor cursor = getActivity().managedQuery(originalUri, imgs, null, null, null); int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(index); } // 把bitmap转换成Base64 String public static String bitmapToString(String filePath) { Bitmap bm = getSmallBitmap(filePath); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 40, baos); byte[] b = baos.toByteArray(); return Base64.encodeToString(b, Base64.DEFAULT); } // 根据路径获得图片并压缩,返回bitmap用于显示 public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location != null) { StringBuffer sb = new StringBuffer(128);// 接受服务返回的缓冲区 // 上传服务器的时候可不能忘记去除\n哦 sb.append(location.getProvince() + location.getCity() + location.getStreet() + "\n" + "(北纬" + location.getLatitude() + "东经" + location.getLongitude() + ")");// 获得城市 loc = sb.toString(); Toast.makeText(getActivity(), loc, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "无法定位", Toast.LENGTH_SHORT) .show(); return; } } @Override public void onReceivePoi(BDLocation arg0) { } } /** * 停止,减少资源消耗 */ public void stopListener() { if (mLocationClient != null && mLocationClient.isStarted()) { mLocationClient.stop();// 关闭定位SDK mLocationClient = null; } } public void initview() { mCurrentMode = LocationMode.NORMAL; mLocationClient = new LocationClient(getActivity()); // 声明LocationClient类 mLocationClient.registerLocationListener(myListener); // 注册监听函数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开GPS option.setAddrType("all");// 返回的定位结果包含地址信息 option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02 option.disableCache(false);// 禁止启用缓存定位 option.setPoiExtraInfo(true);// 获取地址 电话等信息 option.setPriority(LocationClientOption.NetWorkFirst);// 网络定位优先 mLocationClient.setLocOption(option);// 使用设置 mLocationClient.start(); mLocationClient.requestLocation(); } @Override public void onDestroy() { stopListener();// 停止监听 super.onDestroy(); }
}
-
- qq_30578229 2015.12.07 10:53
我以前也遇到过
option.setAddrType("all"); // 定位的街道类型,只有设定为all才可以全部显示
你看这条指令你加了吗
-
- qq_36992605 2017.12.31 12:47
你这跟我的一样 哈哈 我也是遇到了这个鬼东西 我昨天还是有数据的 今天就全部是null 但是经纬度什么的 又有值 很奇怪 我要郁闷死了 你是怎么解决的
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!