0
我正在構建一個需要打開相機制作的圖片的應用程序。我跟着官方Android guide並沒有像他們說的,但我不斷收到此錯誤信息:FileNotFound當在Android中打開圖片時
Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Pictures/JPEG_test_-1259913402.jpg: open failed: ENOENT (No such file or directory)
我的代碼如下所示:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
System.err.println("An error occurred: " + ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//try {
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
System.out.println(imageBitmap);
testViewer.setImageBitmap(imageBitmap);
//bc = new BarDecoder(imageBitmap);
//} catch (NotFoundException | FormatException e){
// System.err.println("An error occurred: " + e);
//}
} else {
System.out.println("Cancelled!");
}
}
public void scanImage(View view){
dispatchTakePictureIntent();
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = "test";
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
System.out.println("Created!!");
return image;
}
我可以看到,畫面的確創建時使用文件瀏覽器,但我的應用程序無法找到它們。我還在我的清單文件中包含了這些權限:
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
有什麼不對?