【发布时间】:2016-09-21 18:36:54
【问题描述】:
final List<Intent> cameraIntents = new ArrayList<Intent>(); final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = getPackageManager(); final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); for (ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent intent = new Intent(captureIntent); intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); intent.setPackage(packageName); cameraIntents.add(intent); } Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); startActivityForResult(chooserIntent, 1);
所以,这就是我打开相机或拍照的方式。我如何在 onactivityresult 上阅读它们????检查下面的代码。问题是,当从照片中选择图像时它可以工作,从相机拍摄时它可以工作。但是当从图库文件夹中选择时,由于某种原因它不起作用。还有一些客户报告我在索尼 xperia e4 手机上的问题。
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (resultCode == Activity.RESULT_OK && requestCode == 1 ) { Bitmap bm = null; try { Bundle extras = data.getExtras(); bm = (Bitmap) extras.get("data"); } catch(Exception e) { try { Uri selectedImage = data.getData(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inDither = true; String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query( selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Common.setBitmap(null); bm = BitmapFactory.decodeFile(filePath); BitmapFactory.decodeFile(Common.getRealPathFromURI(data.getData(), rootView.getContext()), bounds); if(bm == null) bm = BitmapFactory.decodeFile(Common.getRealPathFromURI(selectedImage, AddStoreActivity.this), options); if(bm == null) bm = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage); } catch(Exception e1) { } } } } catch (Exception e) { } } public static String getRealPathFromURI(Uri contentURI, Context cont) { Cursor cursor = cont.getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { // Source is Dropbox or other similar local file path return contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaColumns.DATA); return cursor.getString(idx); } }
【问题讨论】: