从保存位置检索文件时出现NullPointerException

时间:2013-05-06 18:22:41

标签: android file-io nullpointerexception

我正在构建一个Android应用程序拍摄照片,然后立即进入裁剪屏幕。我无法弄清楚如何在不降级的情况下传递图像(即如果我将它放入Bundle中),所以我决定保存图像并将其从卡上拉回来。

现在,当我试图将图像恢复到新的crop_view中时,该视频紧跟在camera_view之后,应用程序会在下面的代码中指示的行上抛出Null Pointer Exception。图像实际上是在Pictures / MyApp / IMG_APP.jpg中设置的,其中“图片”是我的默认图片目录。

public Bitmap getImage() {
    Bitmap toReturn = null;

    File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    ImageView IV = (ImageView) findViewById(R.id.crop_view);
    toReturn = BitmapFactory.decodeFile(root+"/MyApp/IMG_APP.jpg");
    IV.setImageBitmap(toReturn);
    return toReturn;// TODO Fix this line. It is breaking here with a null pointer exception.
}

下面是保存图片的代码。我已经确认通过检查另一个应用程序中的实际SD卡来保存它。

    void onPictureJpeg(byte[] bitmapdata, Camera camera) {
        int i = bitmapdata.length;
        Log.d(TAG, String.format("bytes = %d", i));

        File f = IMGP_Photo_Handler.generateTimeStampPhotoFile();

        try {
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(f));
            outputStream.write(bitmapdata);
            outputStream.flush();
            outputStream.close();
            exceptionCaught = false;

        } catch (Exception e) {
            Log.e(TAG, "Error accessing photo output file:" + e.getMessage());
            Toast
              .makeText(IMGP_Camera.this, "Cannot save file. \nPlease mount an external SD Card", Toast.LENGTH_LONG)
              .show();
            exceptionCaught = true;// To get this to not start the next intent if the file save doesn't work.
        }

        if(!exceptionCaught){
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));

        Intent intent = new Intent();
        intent.setClass(IMGP_Camera.this, IMGP_Crop.class);
        intent.putExtra("po","3265695");
        startActivity(intent);
        }
        finish();

    }

最后我的photo_handler:

    public static Uri photoFileUri = null;

    public static File getPhotoDirectory()  {
        File outputDir = null;
        String externalStorageState = Environment.getExternalStorageState();
        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
            File pictureDir =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            outputDir = new File(pictureDir, "MyApp");
            if(!outputDir.exists())  {
                if(!outputDir.mkdirs()) {
                    String message = "Failed to create directory:" + outputDir.getAbsolutePath();
                    Log.e(TAG, message);
                    outputDir = null;
                }
            }
        }

        return outputDir;
    }

    public static File generateTimeStampPhotoFile() {
        File photoFile = null;
        File outputDir = getPhotoDirectory();

        if (outputDir != null) {
            String photoFileName =  "IMG_APP.jpg";
            photoFile = new File(outputDir, photoFileName);
        }

        return photoFile;
    }

    public static Uri generateTimeStampPhotoFileUri() {
        photoFileUri = null;
        File photoFile = generateTimeStampPhotoFile() ;

        if (photoFile != null) {
            photoFileUri = Uri.fromFile(photoFile);
        }

        return photoFileUri;
    }

    public static Uri getFile(){
        return photoFileUri;
    }

1 个答案:

答案 0 :(得分:1)

我通过将路径传递给照片并使用额外的uri来修复此问题:

String photoFileName =  "IMG_TQL.jpg";
photoFile = new File(outputDir, photoFileName);
photoFilePath = photoFile.getAbsolutePath();


Intent intent = new Intent();
intent.setClass(IMGP_Camera.this, IMGP_Crop.class);
intent.putExtra("path", path);
intent.putExtra("uri", uri);
startActivity(intent);