【问题标题】:cannot access gallery android无法访问画廊 android
【发布时间】:2017-05-01 12:41:28
【问题描述】:

我正在使用此代码从画廊中挑选图片,但现在它已经停止工作了

(items[item].equals("Change from Library")) {
                        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, SELECT_FILE); 

onActivity结果

 Uri selectedImage = data.getData();
                String url = "";
                String pictureDirectoryPath= Functions.getRealPathFromURI(getActivity(),selectedImage()); 

这是我点击选择图片库的按钮时的 Android 监控日志

58.819 8995-8995/com.example.sharique.loginapp W/EGL_genymotion: eglSurfaceAttrib not implemented
05-01 08:39:00.375 557-683/system_process W/InputMethodManagerService:     Window already focused, ignoring focus gain of:     com.android.internal.view.IInputMethodClient$Stub$Proxy@52921524 attribute=null,      token = android.os.BinderProxy@5296999c

【问题讨论】:

  • 提供更好的错误日志。
  • 你也拥有它的所有权限吗?
  • 为什么你认为 selectedImage uri 指向某个路径?
  • 详细解释,“它已停止工作”是什么意思。
  • @Arslan.H 是的,我有权限

标签: android android-intent android-gallery


【解决方案1】:

试试这个:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    startActivityForResult(photoPickerIntent, 500);

onActivity结果

Uri selectedImageUri = imageReturnedIntent.getData();
String mPath = getPath(selectedImageUri);

getPath() 方法

public String getPath(Uri uri) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String imagePath = cursor.getString(column_index);

        return cursor.getString(column_index);
    }

如果您的目标和编译 sdk 高于棒棒糖,那么您必须添加请求权限代码参考 this 链接

【讨论】:

  • 请注意,您的 getPath() 方法将返回无用的值,例如您无权访问的可移动存储上的文件的路径。
  • 没有找到任何帮助:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多