从android 5.0中的库中选择照片

时间:2014-11-20 11:06:47

标签: android photo-gallery

我通过使用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相册是Android 5.0上的默认图库应用。 可能这个问题取决于应用程序还是新的Android操作系统分发问题?

修改

我理解问题:Google相册会自动浏览云服务器上备份图片的内容。事实上,如果我关闭每个互联网连接,并且在选择图像后尝试通过@maveň进行实践,则不会通过从InputStream解码Bitmap来获得结果。

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

2 个答案:

答案 0 :(得分:36)

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

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

这里要处理意图的结果,如本post所述,请注意getPath()函数的工作原理与android构建版本不同:

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

多数人!!

答案 1 :(得分:6)

试试这个:

//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了解更多详情