android - BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions) 返回 FileNotFoundException

标签 android android-imageview android-bitmap bitmapfactory

我正在尝试显示通过存储在外部 SD 卡 中的相机 Intent (触发内置相机应用程序)捕获的图片。

文件路径和文件名存储在字符串变量'mCurrentPhotoPath'中。

我已使用 ES 文件资源管理器 应用验证捕获的图像存储在 'mCurrentPhotoPath' 指定的位置,但是 BitmapFactory.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);

}

解码缩放图像以在 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) ;

    }

但是,BitmapFactory.decodeFile 无法在 mCurrentPhotoPath 中存储的位置找到文件,导致 FileNotFoundException

以下是 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

我正在 Android Kitkat 4.4.4 手机中调试这个应用程序。

请建议在 BitmapFactory.decodeFile 方法中需要进行哪些修改,以便它可以获取存储的文件并将其显示在 ImageView 中。

提前致谢。

最佳答案

您的 mCurrentPhotoPath 有一个前缀“file:”。

因为系统正在尝试引用 /file:/storage/sdcard0/etc...,它指向没有安装的磁盘。

删除它并重试

mCurrentPhotoPath = "/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg";

或者,如果您的 mCurrentPhotoPath 始终具有前缀,请在使用前执行此操作

mCurrentPhotoPath = mCurrentPhotoPath.replace("file:", "");

希望对您有所帮助! :)

关于android - BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions) 返回 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522066/

相关文章:

android - 如何在 android 主题声明中添加自定义项?

php - 使用json从PHP-MySql服务器获取图像到Android

java - 哪些 Canvas 和 Bitmap 方法通过 dpi (Android) 缩放?

java - Android 位图保存错误。保存图片错误,将图片保存到sd卡后更新图库

java - 出现错误 GoldfishAddressSpaceHostMemoryAllocator : ioctl_ping failed for device_type=5, ret=-1

android - 如何让 1024 x 600 的 Android 模拟器工作?

PHP 无法在我的 XAMPP 服务器中正确存储阿拉伯语/波斯语文本

android - 如何在从url加载的ImageView中淡入图片

android - 使用 Universal Image Loader 在 GridView 中加载图像

java - fragment 中的 ImageView 未显示