我尝试从图库中获取图片并在ImageView中显示该图片,但收到此错误:
E/ActivityManager: Sending non-protected broadcast com.motorola.motocare.INTENT_TRIGGER from system 4425:com.motorola.process.system/1000 pkg com.motorola.motgeofencesvc
java.lang.Throwable
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18179)
at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18779)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:512)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2905)
at android.os.Binder.execTransact(Binder.java:565)
04-27 17:21:28.696 559-559/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
这里的完整代码如下:
main.java
package omcommunication.image;
public class AddWOD extends Fragment implements View.OnClickListener {
Activity activity;
public AddWOD() {
}
; private static int IMG_RESULT = 1;
String ImageDecode;
ImageView imageViewLoad;
Button LoadImage;
Intent intent;
String[] FILE;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_wod, container, false);
img=(ImageView) view.findViewById(R.id.imgview);
buy_image1 = (ImageView) view.findViewById(R.id.rent_iv_addpro_image1);
buy_image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG_RESULT);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == IMG_RESULT && resultCode == RESULT_OK
&& null != data) {
Uri URI = data.getData();
String[] FILE = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(URI,
FILE, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(FILE[0]);
ImageDecode = cursor.getString(columnIndex);
cursor.close();
img.setImageBitmap(BitmapFactory
.decodeFile(ImageDecode));
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Please try again", Toast.LENGTH_LONG)
.show();
}
}
我提到很多问题,但没有得到所需的东西,是的,我在清单中给了外部存储权限。
答案 0 :(得分:1)
如果您运行的设备大于Lollipop
,那么您必须缺少Android Runtime Permissions
。
您可以参考here获取答案。
答案 1 :(得分:0)
最好将Uri
交给an image-loading library,例如Picasso,它不仅可以为您处理所有图像加载,还可以在后台线程上执行此操作。
如果你坚持自己做这些事情,请从:
开始@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == IMG_RESULT && resultCode == RESULT_OK
&& null != data) {
imageViewLoad.setImageBitmap(BitmapFactory
.decodeStream(getContentResolver().openInputStream(data.getData()));
}
} catch (Exception e) {
Toast.makeText(this, "Please try again", Toast.LENGTH_LONG)
.show();
}
}
然后,在您开始工作之后,切换到使用AsyncTask
或其他东西在后台线程上执行decodeStream()
部分并在主应用程序线程上执行setImageBitmap()
部分。
特别是,这不应该要求您使用运行时权限。正确使用Uri
可以利用您在用户选择图片时获得的临时权限授予。
答案 2 :(得分:0)
允许运行时权限的代码
if(Build.VERSION.SDK_INT> = 23)
{ checkMultiplePermissions();
} private void checkMultiplePermissions(){
if (Build.VERSION.SDK_INT >= 23) {
List<String> permissionsNeeded = new ArrayList<String>();
List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE)) {
permissionsNeeded.add("Access Location");
}
if (permissionsList.size() > 0) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
}
}
}
private boolean addPermission(List<String> permissionsList, String permission) {
if (Build.VERSION.SDK_INT >= 23)
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<String, Integer>();
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
return;
} else {
// Permission Denied
if (Build.VERSION.SDK_INT >= 23) {
}
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
答案 3 :(得分:0)
它对我的工作:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImg = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImg,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
buy_image1 .setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close();
}
}