我有一個應用程序可以使用這樣的攝像頭捕捉圖像。文件在android中不存在?
String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, galleryPermissions)){
//Camera
if (index == 0) {
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) {
Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = null;
// N is for Nougat Api 24 Android 7
if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) {
// FileProvider required for Android 7. Sending a file URI throws exception.
photoURI = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
Log.v("highbuild",photoURI.toString());
} else {
// For older devices:
// Samsung Galaxy Tab 7" 2 (Samsung GT-P3113 Android 4.2.2, API 17)
// Samsung S3
photoURI = Uri.fromFile(photoFile);
Log.v("lowbuild",photoURI.toString());
}
imageuriduplicate = photoURI;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 0);
}
else{
Log.v("file", "photo file is null");
Toast.makeText(getApplicationContext(),"Photo file is null", Toast.LENGTH_LONG).show();
}
}
}
//Gallery
else if (index == 1) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
pickPhoto.setType("image/*");
startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
}
}
else {
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
}
我創建的文件圖像的方法是這樣的
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "AndroidUpload" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if(!storageDir.exists()){
boolean s = new File(storageDir.getPath()).mkdirs();
if(!s){
Log.v("not", "not created");
}
else{
Log.v("cr","directory created");
}
}
else{
Log.v("directory", "directory exists");
}
File image = File.createTempFile(
imageFileName, /* prefix */
".png", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
我那麼路徑後保存到一個ArrayList和訪問它。
但是當我嘗試訪問該文件這樣
Uri imageuri = Uri.parse(receipts.get(i).imageURI);
File file = new File(imageuri.getPath());
if(file.exists()){
Log.v("exists","file exists");
}
else{
Log.v("no","file doesnt exist");
}
我得到犯規存在錯誤的文件。
我給了所有必要的權限,我仍然收到此錯誤。如何解決這個問題?
一個Uri(自己的應用程序的Android類?)可能包含'「file:...」'。 –
您沒有檢查mkdirs()的返回值。此外,你應該直接檢查文件的存在性。不是在你搞砸數據庫之後。 – greenapps
如果mkdirs()返回false,則應該拋出IOException。或者返回null。 – greenapps