Android 从保存位置检索文件时出现NullPointerException
我正在构建一个Android应用程序,它可以拍照,然后立即进入屏幕。我不知道如何在不降级的情况下传递图像(例如,如果我将其打包),因此我决定保存图像并将其从卡中取出 现在,当我试图将图像恢复到新的裁剪视图中时,该视图正好位于摄影机视图之后,应用程序会在下面代码中指示的行上抛出一个空指针异常。图像实际上设置在Pictures/MyApp/IMG_APP.jpg,其中“Pictures”是我的默认图片目录Android 从保存位置检索文件时出现NullPointerException,android,file-io,nullpointerexception,Android,File Io,Nullpointerexception,我正在构建一个Android应用程序,它可以拍照,然后立即进入屏幕。我不知道如何在不降级的情况下传递图像(例如,如果我将其打包),因此我决定保存图像并将其从卡中取出 现在,当我试图将图像恢复到新的裁剪视图中时,该视图正好位于摄影机视图之后,应用程序会在下面代码中指示的行上抛出一个空指针异常。图像实际上设置在Pictures/MyApp/IMG_APP.jpg,其中“Pictures”是我的默认图片目录 public Bitmap getImage() { Bitmap toReturn
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();
}
最后是我的照片处理程序:
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);
我通过在一个额外的文件中传递照片和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);
您是否添加了这些权限
读取外部存储
或写入外部存储
?因为从API 16开始,如果您的设备上启用了保护USB存储,那么如果没有这些权限之一,您将无法访问外部存储。是的,我添加了读取外部存储您是否添加了这些权限READ\u external\u storage
或WRITE\u external\u storage
?因为从API 16开始,如果您的设备上启用了保护USB存储,那么如果没有这些权限,您将无法访问外部存储。是的,我已添加了读取外部存储