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

标签 android file-io nullpointerexception

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

现在,当我试图在 camera_view 之后的新 crop_view 中取回图像时,应用程序在下面代码中指示的行上抛出空指针异常。图片实际上是在 Pictures/MyApp/IMG_APP.jpg 中设置的,其中“Pictures”是我的默认图片目录。

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;
    }

最佳答案

我通过额外传递照片和 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);

关于android - 从保存位置检索文件时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16404787/

相关文章:

java - xml 中的自定义值

android edittext在键盘打开时变得部分可见

java - 使用按钮标签,我可以自定义按钮吗?

Android map 缩放至覆盖项目

C++:这是在Windows平台上检查文件存在的最佳方法

java - 我的代码以某种方式返回 null,我不知道为什么

python - 多次运行计算数学函数 (Numpy)

linux - 用于查找硬件信息的 Bash 脚本给出 "Memory Fault"

android - 在 FragmentStatePagerAdapter NPE 中运行数据库查询

java - Spring中无法findAll,出现空指针异常