I am trying to display picture captured through camera intent (triggering inbuilt camera app) which is stored in external SD-card
.
我正在尝试通过相机的意图来显示照片(触发内置摄像头应用),它存储在外部sd卡中。
The file path and file name is stored in String variable 'mCurrentPhotoPath'
.
文件路径和文件名存储在字符串变量'mCurrentPhotoPath'中。
I have verified that the image captured is stored in the location specified by 'mCurrentPhotoPath'
using ES File Explorer
app, but the BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)
is always returning FileNotFoundException
. Here is my code to store the captured image:
我已经验证了所捕获的图像存储在“mCurrentPhotoPath”使用ES文件管理器app指定的位置,但是位mapfactory . decodefile (mCurrentPhotoPath, bmOptions)总是返回FileNotFoundException。下面是我的代码来存储捕获的图像:
private void addPicToGallery() {
File f = new File(mCurrentPhotoPath);
//for debug purpose only
Log.i(MYTAG, "value of mCurrentPhotoPath in addPicToGallery: " +mCurrentPhotoPath);
Log.i(MYTAG, "value of f in addPicToGallery: " +f);
Uri contentUri = Uri.fromFile(f);
//for debug only
Log.i(MYTAG, "value of contentUri in addPicToGallery: " +contentUri);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
Code for decoding a scaled image for display in ImageView
:
用于在ImageView中显示缩放图像的代码:
private void setFullImageFromFilePath() {
Log.i(MYTAG, "Entered setFullImageFromFilePath method");
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
Log.i(MYTAG,"var targetW in setFullImageFromFilePath is:" +targetW);
Log.i(MYTAG,"the targetH in setFullImageFromFilePath is:" + targetH);
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Log.i(MYTAG, "the mCurrentPhotoPath in setFullImageFromFilePath is:" + mCurrentPhotoPath);
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW= bmOptions.outWidth;
int photoH = bmOptions.outHeight;
//Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
//Decode the image file into a Bitmap sized to fill the view
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap) ;
}
However, BitmapFactory.decodeFile
is not able to find the file at the location stored in mCurrentPhotoPath
resulting in FileNotFoundException
.
但是,BitmapFactory.decodeFile无法找到存储在mCurrentPhotoPath中的文件,从而导致FileNotFoundException。
Following is the LogCat output:
以下是LogCat输出:
>07-20 21:28:11.897 32301-32301/org.assignment.lab.Pic I/Lab-Intents﹕ the mCurrentPhotoPath in setFullImageFromFilePath is:file:/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg
07-20 21:28:11.907 32301-32301/org.assignment.lab.Pic E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg: open failed: ENOENT (No such file or directory)
07-20 21:28:11.937 32301-32301/org.assignment.lab.Pic D/AndroidRuntime﹕ Shutting down VM
07-20 21:28:11.937 32301-32301/org.assignment.lab.Pic W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418d0ce0)
07-20 21:28:12.047 32301-32301/org.assignment.lab.Pic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.assignment.lab.Pic, PID: 32301
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {org.assignment.lab.mydailyselfie/org.coursera.assignment.lab.Pic.MainActivity}: java.lang.ArithmeticException: divide by zero
I am debugging this application in Android Kitkat 4.4.4
phone.
我正在Android Kitkat 4.4.4手机上调试这个应用程序。
Please suggest what modification needed in BitmapFactory.decodeFile
method so that it can get the file stored and display it in ImageView
.
请建议在BitmapFactory.decodeFile方法中需要修改什么,以便它能将文件存储并显示在ImageView中。
Thanks in advance.
提前谢谢。
1 个解决方案
#1
8
Your mCurrentPhotoPath
has a prefix "file:
".
您的mCurrentPhotoPath有一个前缀“file:”。
Because of which the system is trying to refer to /file:/storage/sdcard0/etc...
which points to no mounted disk.
因为系统正试图引用/文件:/存储/sdcard0/etc…它指向没有挂载磁盘。
Remove that and try again with
把它拿掉,再试一次。
mCurrentPhotoPath = "/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg";
Or, if your mCurrentPhotoPath
will always have the prefix, do this before using it
或者,如果你的mCurrentPhotoPath总是有前缀,在使用它之前先做这个。
mCurrentPhotoPath = mCurrentPhotoPath.replace("file:", "");
Hope this helps! :)
希望这可以帮助!:)
#1
8
Your mCurrentPhotoPath
has a prefix "file:
".
您的mCurrentPhotoPath有一个前缀“file:”。
Because of which the system is trying to refer to /file:/storage/sdcard0/etc...
which points to no mounted disk.
因为系统正试图引用/文件:/存储/sdcard0/etc…它指向没有挂载磁盘。
Remove that and try again with
把它拿掉,再试一次。
mCurrentPhotoPath = "/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg";
Or, if your mCurrentPhotoPath
will always have the prefix, do this before using it
或者,如果你的mCurrentPhotoPath总是有前缀,在使用它之前先做这个。
mCurrentPhotoPath = mCurrentPhotoPath.replace("file:", "");
Hope this helps! :)
希望这可以帮助!:)