函数无法从内存中的位置重新创建位图

时间:2015-08-17 04:07:48

标签: android bitmap

我有这段代码:

    Bitmap readBitmapImage() {
        String imageInSD = "/sdcard/mac/"+strURL;

        BitmapFactory.Options bOptions = new BitmapFactory.Options();
        bOptions.inTempStorage = new byte[16*1024];

        bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);
        return bitmap;
    }

decodeFile函数返回一个空值,该值被分配给位图。为什么decodeFile没有返回保存在SDCard上的文件的Bitmap图像?

我正在使用AndroidStudio。

1 个答案:

答案 0 :(得分:1)

问题在于您的图像路径,我认为它不会返回绝对路径。因此,请尝试使用此功能获取 imageInSD 路径,如下所示

String imageInSD=new File(getFilesDir(), "test.png").getAbsolutePath();

此处test.png是您的图像名称,并确保它存在于您的文件路径中。

在处理SD卡时,不要忘记在清单中添加这些权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />