如何在安卓中获取特定图像文件夹的缩略图库?

5

我在SD卡的Pictures文件夹中保存了一些图片。我想直接访问我的文件夹中的这些图片。

我已经使用以下代码直接选择图库中的图片:

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setDataAndType(Uri.parse("file:///sdcard/Pictures/"), "image/*");

startActivityForResult(intent, 1);

上面的代码是从SD卡中获取所有图像。但我只需要我的照片文件夹中的图像。我也尝试了Intent.ACTION_GET_CONTENT,但结果相同。
请有经验的人给我指正一下...
谢谢。

你得到答案了吗?如果是,请分享一下。 - PiyushMishra
1个回答

0

这是我用来从SD卡中选择图片的代码

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),JobActivity.SELECT_PHOTO);

请注意,这将加载根文件夹。

一旦选择了照片,将调用onActivityResult方法并获取图像。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        if (resultCode == RESULT_OK) {
            if (requestCode == JobActivity.SELECT_PHOTO) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getPath(selectedImageUri);
                getBitmap(selectedImagePath, 0);
                // Log.d("Debug","Saved...." + selectedImagePath);
            }
        }
    } catch (Exception e) {
        Log.e("Error", "Unable to set thumbnail", e);
    }
}

获取路径方法

public String getPath(Uri uri) {

    Cursor cursor = null;
    int column_index = 0;
    try {
        String[] projection = { MediaStore.Images.Media.DATA };
        cursor = managedQuery(uri, projection, null, null, null);
        column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
    } catch (Exception e) {
        Log.d("Error", "Exception Occured", e);

    }

    return cursor.getString(column_index);
}

最后获取位图。
public Bitmap getBitmap(String path, int size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = size;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return bitmap;
}

size变量允许通过因子缩放图像的大小。如果您不希望进行缩放,请删除选项参数。

我不确定如何告诉它从根目录以外的另一个文件夹中选择。

这里还有一篇有用的文章 从Android内置的画廊应用程序中以编程方式获取/选择图像


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接