Uri.fromFile返回null?

时间:2016-08-10 00:13:10

标签: java android

由于某种原因,outputFileUri在我的onActivityResult中返回null:

Uri selectedImageUri;
if (isCamera) {
    selectedImageUri = outputFileUri;

我不明白为什么。请查看我的代码,看看我哪里出错了。我让用户通过选择图库或使用相机拍摄4张图像。用户使用相机拍摄的照片将无法显示,因为他们的uri为空。 outputFileUri是一个类变量。

takePictureIntent()

private void dispatchTakePictureIntent() {
    for(int i = 0; i < 4; i++) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                outputFileUri = Uri.fromFile(photoFile);
            } catch (IOException ex) {
                Log.w("error","IOException");
            }catch (NullPointerException nullEx) {
                Log.w("error","NullPointerException");
            }
            // Camera.
            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.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);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                cameraIntents.add(intent);
            }
            // Filesystem.
            final Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
            // Chooser of filesystem options.
            final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
            // Add the camera options.
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
            if(id.equals(HAPPY_ID))
                startActivityForResult(chooserIntent, REQUEST_HAPPY_PHOTO);
            if(id.equals(SURPRISED_ID))
                startActivityForResult(chooserIntent, REQUEST_SURPRISED_PHOTO);
            if(id.equals(AFRAID_ID))
                startActivityForResult(chooserIntent, REQUEST_AFRAID_PHOTO);
            if(id.equals(UPSET_ID))
                startActivityForResult(chooserIntent, REQUEST_UPSET_PHOTO);
            if(id.equals(SAD_ID))
                startActivityForResult(chooserIntent, REQUEST_SAD_PHOTO);
        }
    }
}

onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_HAPPY_PHOTO || requestCode == REQUEST_SURPRISED_PHOTO || requestCode == REQUEST_AFRAID_PHOTO ||
                    requestCode == REQUEST_UPSET_PHOTO || requestCode == REQUEST_SAD_PHOTO) {
                final boolean isCamera;
                if (data == null) {
                    isCamera = true;
                } else {
                    final String action = data.getAction();
                    if (action == null) {
                        isCamera = false;
                    } else {
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                }
                Uri selectedImageUri;
                if (isCamera) {
                    selectedImageUri = outputFileUri;
                } else {
                    selectedImageUri = data == null ? null : data.getData();
                }
                //Log.d("doing ids", "right before id");
                //Log.d("doing ids", "id is " + id);
                if(requestCode == REQUEST_HAPPY_PHOTO) {
                    //Log.d("doing ids", "in happy");
                    happyList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_SURPRISED_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    surprisedList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_AFRAID_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    afraidList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_UPSET_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    upsetList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_SAD_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    sadList.add(selectedImageUri);
                }
            }
        }
    }

我的createImageFile():

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp;
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,我通过保存对传递给Intent的URI的引用来解决它。这使我不依赖于Intent来返回URI。

以下是代码:

sed -i 's/\^[^^]//g' infile