【问题标题】:Pick photo from gallery in android 5.0从 android 5.0 中的图库中选择照片
【发布时间】:2014-11-20 11:06:46
【问题描述】:

我在使用 android 5.0 从图库中挑选图像时遇到问题。我的启动意图代码是:

private void takePictureFromGallery() 
{
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(intent, PICK_FROM_FILE);
}

这里是请求代码 PICK_FROM_FILE 的 onActivityResult() 方法中调用的函数

private void handleGalleryResult(Intent data) 
{
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    // field declaration private String mTmpGalleryPicturePath;
    mTmpGalleryPicturePath = cursor.getString(columnIndex);
    cursor.close();
    // at this point mTmpGalleryPicturePath is null
    ...
}

对于 5.0 之前的版本,此代码始终有效,使用 com.android.gallery 应用程序。 Google Photos 是 Android 5.0 上的默认图库应用程序。 这个问题可能取决于应用程序还是新的 android OS 发行版的问题?

编辑

我了解问题所在:Google 相册会自动在云服务器上浏览其备份图像的内容。事实上,如果我关闭每个互联网连接并选择图像后,@maveň 的尝试实践建议,它不会通过从 InputStream 解码位图得到结果。

所以此时问题变成了:在 android 5.0 中有没有办法处理 Intent.ACTION_PICK 动作,以便系统浏览在本地设备图片库中选择?

【问题讨论】:

  • 我很抱歉,但还是不明白......你在 onResult() 中调用的 getPath() 方法是用link 编写的方法(在这种情况下,你的参数语法错误) 还是自己写的函数?无论如何,我尝试获取链接中显示的路径,但没有使用 BitmapFactory 解码流,但不起作用。

标签: android photo-gallery


【解决方案1】:

我结合以下方法找到了解决此问题的方法。 在这里开始从设备库中挑选图像的活动:

private void takePictureFromGallery() 
{
    startActivityForResult(
        Intent.createChooser(
            new Intent(Intent.ACTION_GET_CONTENT)
            .setType("image/*"), "Choose an image"), 
        PICK_FROM_FILE);
}

这里处理意图的结果,如post 中所述,请注意getPath() 函数的工作方式与 android build 版本不同:

private void handleGalleryResult(Intent data) 
{
    Uri selectedImage = data.getData();
    mTmpGalleryPicturePath = getPath(selectedImage);
    if(mTmpGalleryPicturePath!=null)
        ImageUtils.setPictureOnScreen(mTmpGalleryPicturePath, mImageView);
    else
    {
        try {
            InputStream is = getContentResolver().openInputStream(selectedImage);
            mImageView.setImageBitmap(BitmapFactory.decodeStream(is));
            mTmpGalleryPicturePath = selectedImage.getPath();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@SuppressLint("NewApi")
private String getPath(Uri uri) {
    if( uri == null ) {
        return null;
    }

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor;
    if(Build.VERSION.SDK_INT >19)
    {
        // Will return "image:x*"
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];
        // where id is equal to             
        String sel = MediaStore.Images.Media._ID + "=?";

        cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                      projection, sel, new String[]{ id }, null);
    }
    else
    {
        cursor = getContentResolver().query(uri, projection, null, null, null);
    }
    String path = null;
    try
    {
        int column_index = cursor
        .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index).toString();
        cursor.close();
    }
    catch(NullPointerException e) {

    }
    return path;
}
  • takePictureFromGallery()onActivityResult 调用

就是这样!!

【讨论】:

  • 我不认为这是正确的。应该是 if(Build.VERSION.SDK_INT > 19) 而不是 if(Build.VERSION.SDK_INT >=19)
  • 我收到 IllegalArgumentException: Invalid URI: content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A...6mkQk-P4tzU/ACTUAL/11。 ..80 适用于在线存储在 Google 照片中的照片。
  • 很好的解决方案在 Andorid 5.0 中为我工作,从其他文件夹中挑选图像。
【解决方案2】:

试试这个:

//5.0
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, CHOOSE_IMAGE_REQUEST);

在 onActivityResult 中使用以下内容:

Uri selectedImageURI = data.getData();
input = c.getContentResolver().openInputStream(selectedImageURI);
BitmapFactory.decodeStream(input , null, opts);

更新

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Uri selectedImageUri = data.getData();  
String tempPath = getPath(selectedImageUri);

/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
    if( uri == null ) {
        return null;
    }
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
  }

  }  }

tempPath 将存储 ImageSelected 的路径

Check this了解更多详情

【讨论】:

  • 好的,但我如何检索照片的字符串路径?我需要它!
  • @and.ryx string path of photo 是什么意思?
  • 例如,如果文件“picture01.jpg”存储在“sdcard”目录的“Photos”文件夹中,我的字符串输出应该是:“/sdcard/Photos/picture01.jpg”所以路径图片文件...
  • 路径不是固定的,它会随着设备的变化而变化。就像在某些设备中它会是 /sdcard/Photos/picture01.jpg 而在其他设备中是 /sdcard0/Photos/picture01.jpg
  • @and.ryx 调试并检查String 的值 ....我提供的代码对我来说很好,请在设备上测试此代码。
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
相关资源
最近更新 更多