返回null,我用我的意圖以下保存到應用程序目錄的圖像(從Android開發網站得到它):保存圖片的應用程序目錄中的onActivityResult
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。我做錯了嗎?我從我的onActivityResult()
撥打setCameraPicture()
,並將Intent data
作爲參數傳入。
如果我不保存到應用程序目錄,此代碼工作正常。你們有沒有任何建議?任何幫助將不勝感激。預先感謝您
您正在運行此代碼的設備模型是什麼? – Melquiades
@Melquiades目前我正在測試三星Galaxy S3 LTE – Lunchbox
在啓動相機意圖之前,請保存對File對象的引用。然後在活動結果上使用bitmapfactory。 – Slynk