允许用户从相机和图库中选择图像不适用于已安装的应用,如Camera360

时间:2015-06-12 14:57:37

标签: android image camera

我是Android开发中的新手。我的应用程序允许用户从图库中选择图像并捕获从相机拍摄的图像。从图库和原生相机中挑选图像时,它可以正常工作,但是当从像Camera360这样的已安装应用中选择图像时,它无法正常工作。任何人都可以帮我解决这个问题。

下面是我的代码,显示从图库和相机中选择图像的选项

    File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator);
    if (!root.exists())
        root.mkdirs();
    String fname = "img_" + System.currentTimeMillis() + ".jpg";
    File sdImageMainDirectory = new File(root, fname);
    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.
    List<Intent> cameraIntents = new ArrayList<Intent>();
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        String packageName = res.activityInfo.packageName;
        Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }
    // Filesystem.
    Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    // Chooser of filesystem options.
    Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    startActivityForResult(chooserIntent, PICK_IMAGE_REQUEST);

onActivityResult方法在下面实现

          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_IMAGE_REQUEST) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else
                    isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
            }
            Bitmap yourSelectedImage;
            if (isCamera) {

                yourSelectedImage = BitmapFactory.decodeFile(outputFileUri.getPath());
            } else {
                Uri selectedImage = data.getData();
                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();
                yourSelectedImage = BitmapFactory.decodeFile(filePath);
            }
            com.pkmmte.view.CircularImageView CircularImageView = (com.pkmmte.view.CircularImageView) findViewById(R.id.profileImage);
            CircularImageView.setImageBitmap(yourSelectedImage);
        }
    }
}

代码适用于相机和图库图像选择器。但它像Camera360这样安装的应用程序崩溃了。有人可以帮我解决这个问题吗

1 个答案:

答案 0 :(得分:1)

Uri不是文件,因此您无法将其传递给decodeFile()上的BitmapFactory。您“假装此Uri实际上来自MediaStore”代码也无效。

使用像Picasso或Universal Image Loader这样的图像加载库,或者使用openInputStream()上使用ContentResolver的自己的后台线程来读取Uri的内容,传递decodeStream()上的BitmapFactory流。

  

该代码适用于相机和图库图像选择器。

仅适用于您尝试过的几个案例。例如,如果“图库图像选择器”返回Android 4.4+上可移动存储上的图像,即使您可能获取文件系统路径,也无法读取它,因为您不能t对可移动存储上的任意位置具有读访问权。