Android:从Gallery获取Thumbnailpath和Imagepath

时间:2014-09-17 12:08:19

标签: android android-cursorloader

我很少陷入疑问。搜索了很多,但没有得到理想的结果。所以请帮助我解决这个问题..

我想从图库中选择图像并且我在其中取得了成功..但现在我也希望获得缩略图路径。
我知道我可以从图像路径创建缩略图...但是我想要缩略图的字符串路径所以我可以在数组中使用它....

所以当我从图库中选择任何图像时,我想要这两条路径..

  1. 图片路径
  2. 缩略图路径所选图片的ID
  3. 我的代码..

    打开图库..

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

    OnActivityResult()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        path = null;
    
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != 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]);
            path = cursor.getString(columnIndex);
            long imageId = cursor.getLong(columnIndex); //getting 0 here... i thought it could give me image id.. :p 
            Log.v("AddEventDataActivity", "Selected Image Path : " + path); // getting image path successfully...
            Log.v("AddEventDataActivity", "Selected Image ID : " + imageId); // ??? 
            cursor.close();
    
            // tried this but not succeed.. :(
            Cursor thumbcursor = MediaStore.Images.Thumbnails
                    .queryMiniThumbnail(getContentResolver(), imageId,
                            MediaStore.Images.Thumbnails.MINI_KIND, null);
    
            if (thumbcursor != null && thumbcursor.getCount() > 0) {
                thumbcursor.moveToFirst();// **EDIT**
                thumbpath = thumbcursor.getString(thumbcursor
                        .getColumnIndex(MediaStore.Images.Thumbnails.DATA));
            }
            thumbcursor.close();
    
            Log.v("THUMB", "THUMBNAIL PATH : " + thumbpath); // no value to thumbpath...
    
        } 
    }
    

    我找不到如何解决这个问题...... 任何帮助都会有所帮助......

2 个答案:

答案 0 :(得分:0)

获取缩略图

Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                             getContentResolver(), selectedImageUri,
                             MediaStore.Images.Thumbnails.MINI_KIND,
                             (BitmapFactory.Options) null );

其中selectedImageUri = data.getData();

答案 1 :(得分:0)

我知道这是旧的,但这对我有用:

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        try {
            Long thumbId = Long.parseLong(selectedImage.getLastPathSegment());

            Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                    getContentResolver(),
                    thumbId,
                    MediaStore.Images.Thumbnails.MINI_KIND,
                    null
            );
        } catch (NumberFormatException e) {
            //Handle exception
        }
    }
}