BitmapFactory 解码 returns 空

BitmapFactory decode returns null

我现在正在 android 处理一件奇怪的事情。直到几天前我的代码才停止工作。 我正在从图库中选择一个图像并想要获取它的 Bitmap 对象。 我的代码:

 imgDecodableString = "/storage/emulated/0/DCIM/Camera/IMG_20160114_141351594.jpg"

 Bitmap decodedBitmap = BitmapFactory.decodeFile(imgDecodableString);
 Log.d(Constants.TAG, "decodedBitmap: " + decodedBitmap);

位图现在为空(以前可以工作),我真的不明白哪里出了问题。没有改变我的代码。有人有这个问题吗?谢谢。

我有权限:

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

您需要删除第一行的注释,它应该如下所示

imgDecodableString = "/storage/emulated/0/DCIM/Camera/IMG_20160114_141351594.jpg"

Bitmap decodedBitmap = BitmapFactory.decodeFile(imgDecodableString);
Log.d(Constants.TAG, "decodedBitmap: " + decodedBitmap);

请使用以下代码:

if(imgDecodableString.exists()){
Bitmap decodedBitmap = BitmapFactory.decodeFile(imgDecodableString);
Log.d(Constants.TAG, "decodedBitmap: " + decodedBitmap);}

希望对您有所帮助。

if (CheckPermission(youractivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// you have permission go ahead

String  imgDecodableString = "/storage/emulated/0/DCIM/Camera/IMG_20160114_141351594.jpg";

if(new File(imgDecodableString).exists()){
Bitmap decodedBitmap = BitmapFactory.decodeFile(imgDecodableString);
Log.d("Tag", "decodedBitmap: " + decodedBitmap);}
} else {
// you do not have permission go request runtime permissions
RequestPermission(youractivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
}



@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

    switch (permsRequestCode) {

        case REQUEST_RUNTIME_PERMISSION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                // you do not have permission show toast.
            }
            return;
        }
    }
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
    if (ContextCompat.checkSelfPermission(thisActivity,
            Permission)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Permission)) {

        } else {
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Permission},
                    Code);
        }
    }
}

public boolean CheckPermission(Activity context, String Permission) {
    if (ContextCompat.checkSelfPermission(context,
            Permission) == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

在 androidmanifest 中

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