android - 为什么 Bitmap.decodeFile() 方法返回 null?

标签 android bitmap bitmapfactory

我正在尝试像这样将文件解码为位图 -

 BitmapFactory.Options options = new BitmapFactory.Options();
    try {
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
        cropImageView.setImageBitmap(bitmap);
        Log.d("Musik","bitmap - "+bitmap+" "+mCurrentPhotoPath);
    }
    catch (Exception e){
        Log.d("Musik",e.toString());
    }

我的 mCurrentPhotoPath 是

file:/storage/sdcard0/Android/data/com.example.iaugmentor.iaugmentortestvers/files/Pictures/JPEG_20160805_161105_478260200.jpg

我无法找到它返回 null 的原因,也没有捕获到异常。

请帮忙。提前致谢。

为了清楚起见,让我首先说明我是如何保存图像的,我首先创建一个 File 对象,然后捕获图像并将其保存在给定的 Uri 中 -

// Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        // Error occurred while creating the File

                    }

                    if (items[which].equals("Take Photo")) {

                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                        if(intent.resolveActivity(getPackageManager()) != null) {


                            // Continue only if the File was successfully created
                            if (photoFile != null) {
                                profilePicUri = FileProvider.getUriForFile(ApplicationContext.getAppContext(),
                                        "com.example.iaugmentor.iaugmentortestvers",
                                        photoFile);


                            }
                            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, profilePicUri);

                            try {
                                intent.putExtra("return-data", true);

                                startActivityForResult(intent, RESULT_LOAD_IMG);
                            } catch (ActivityNotFoundException e) {
                                e.printStackTrace();
                            }
                        }

createImageFile() 方法是这样的`

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(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();
    return image;
}

然后我尝试在变量mCurrentPhotoPath

的帮助下从另一个 Activity 中读取图像

最佳答案

在您的 createImageFile() 方法中创建/保存文件路径:mCurrentPhotoPath

File imageFile = new File(image.getAbsolutePath()); 

mCurrentPhotoPath = imageFile.toString();

关于android - 为什么 Bitmap.decodeFile() 方法返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787483/

相关文章:

android - 如何检测android设备的方向?

android - 为什么用inBitmap解码位图总是得到java.lang.IllegalArgumentException : P ro blem decoding into existing bitmap

java - 如何避免 java.lang.OutOfMemoryError : bitmap size exceeds VM budget in android while using a large bitmap?

android - 支持的最大图像尺寸是多少

android - 位图被垃圾收集后是否可以使用?

android - 为什么 BitmapFactory.decodeResource 会缩放我的图像?

java - Android RSS Xml 错误

java - 直接绘制到 Canvas 并使用 java 代码创建布局

android - 媒体播放器实例 : stop behaves like a pause

javascript - 使用 JavaScript 获取(索引)位图的颜色索引