基本上我想要做的是創建一個簡單的應用程序,它使用默認的攝像頭應用程序拍照,然後我想在ImageView中的新活動中顯示該圖像,但是我什麼都試過導致無法在ImageView中顯示攝像機活動的結果
java.io.FileNotFoundException: /storage/emulated/0/imageToProcess.jpg: open failed: ENOENT (No such file or directory)
我已經包含了正確的權限(甚至太多)在清單:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
,我也請求手動權限,如果需要在t中他的主要活動:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
}
所以我不認爲這個問題是與讀/寫權限。
在我的主要活動的XML文件,我創建了一個按鈕,一個onClick監聽器調用下面的方法:
public void openCamera(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "imageToProcess.jpg");
imgUri = Uri.fromFile(f);
intent.putExtra("imgUri", imgUri.toString());
startActivityForResult(intent, TAKE_PICTURE);
}
這是我定義爲創建一個新的意圖和傳遞的onActivityResult()方法URI到新的活動:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
Intent camResult = new Intent(this, ShowCameraResult.class);
camResult.putExtra("imgUri", imgUri.toString());
startActivity(camResult);
}
}
注:imgUri和TAKE_PICTURE在類的頂部定義爲protected static final int TAKE_PICTURE = 1;
和private Uri imgUri;
最後,這裏是我的ShowCameraResult活動
public class ShowCameraResult extends AppCompatActivity {
private Bitmap mImageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_camera_result);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
String filePath = getIntent().getStringExtra("imgUri");
Log.d("ShowCameraResult", "directory: " + filePath);
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(filePath));
imageView.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
的Log.d方法調用上述返回
ShowCameraResult: directory: file:///storage/emulated/0/imageToProcess.jpg
這使我相信,從攝像頭的圖像結果是不實際被保存到我的imageToProcess.jpg文件,但我可能是非常錯誤的,因爲這是我工作的第一個真正的Android項目。
所以我的問題是:我哪裏錯了?
任何幫助非常感謝,謝謝!
測試設備上不是模擬器 –
@Dhara我在銀河S5 – Austin
它對其他三星測試中的錯誤檢測設備 –