将图片保存到app目录会在onActivityResult中返回null

时间:2014-01-17 13:28:02

标签: android android-intent

我将图像保存到应用程序目录中,使用以下内容与我的意图(从Android开发站点获取):

private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getActivity().getApplicationContext().getFilesDir();
        File image = File.createTempFile(imageFileName, ".jpg", storageDir);
        return image;
    }

然后在onClick

File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException e) {
                    Toast.makeText(getActivity().getApplicationContext(), "File not created", Toast.LENGTH_LONG).show();
                }
                if (photoFile != null){
                    Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    getActivity().startActivityForResult(camIntent, 0);
                }

到这里它完美无缺。我使用我的文件管理器检查了应用程序目录,图片被保存,其他应用程序无法看到它们,因为它应该是。问题是,当我尝试在ImageView中显示它时,我会得到一个NullPointerException。这是问题代码:

private void setCameraPicture(int picture_count, Intent data) {
        // TODO Auto-generated method stub
        Bitmap bm = null;
        try {
            Uri selectedImage = data.getData();
            Toast.makeText(getApplicationContext(), "picture = " + selectedImage, Toast.LENGTH_LONG).show();
            String camPictureString = getRealPathFromURI(selectedImage);
            File f = new File(camPictureString);
            ExifInterface ei = new ExifInterface(camPictureString);
            int rotation = 0;
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                rotation = 90;
                bm = rotateCameraImage(rotation, f);
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                rotation = 180;
                bm = rotateCameraImage(rotation, f);
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                rotation = 270;
                bm = rotateCameraImage(rotation, f);
            } else {
                bm = rotateCameraImage(rotation, f);
            }
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "IOException", Toast.LENGTH_LONG).show();
        } catch (NullPointerException e) {
            // TODO: handle exception
            Toast.makeText(getApplicationContext(), "NullPointerException", Toast.LENGTH_LONG).show();

        }
    }

Uri selectedImage = data.getData();返回null。我做错了吗?我从setCameraPicture()拨打onActivityResult()并将Intent data作为参数传递给我。

如果我不保存到应用程序目录,此代码可以正常工作。你们有什么建议吗?任何帮助将不胜感激。提前谢谢

1 个答案:

答案 0 :(得分:0)

getData()返回null,因为您已指定自定义文件以将图像保存在(Media.EXTRA_OUTPUT)中。数据存储在您指定的路径中,而不会在onActivityResult()中返回。这是有道理的,因为如果数据已存储在某处,则没有理由返回它。只需从路径中检索位图图像。