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;
}
}
在androidmainfest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果您使用的Android 6.0比你需要運行時寫入外部存儲 –