我正在处理android中的一个奇怪的事情。我的代码以前工作到几天前停止工作。 我从库中选择一个图像,并希望得到它的Bitmap对象。 我的代码:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
位图现在为空(用于工作),我真的不知道出了什么问题。没有改变我的代码。有人有这个问题吗?谢谢。
我有权限:
private HashSet<TextView> getTextViews(ViewGroup root){
HashSet<TextView> views=new HashSet<>();
for(int i=0;i<root.getChildCount();i++){
View v=root.getChildAt(i);
if(v instanceof TextView){
if (((TextView) v).getText().equals("YOUR_STRING")){
views.add((TextView)v);
}
}
}
return views;
}
答案 0 :(得分:1)
您需要从第一行删除评论,它应该如下所示
imgDecodableString = "/storage/emulated/0/DCIM/Camera/IMG_20160114_141351594.jpg"
Bitmap decodedBitmap = BitmapFactory.decodeFile(imgDecodableString);
Log.d(Constants.TAG, "decodedBitmap: " + decodedBitmap);
答案 1 :(得分:1)
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"/>
答案 2 :(得分:0)
请使用以下代码:
if(imgDecodableString.exists()){
Bitmap decodedBitmap = BitmapFactory.decodeFile(imgDecodableString);
Log.d(Constants.TAG, "decodedBitmap: " + decodedBitmap);}
希望它会有所帮助。