如何在Android中获取特定图像文件夹的Galley Thumbnail?

mar*_*ran 5 android thumbnails gallery sd-card subdirectory

我在SD卡的图片文件夹中保存了一些图像.我想直接访问我的文件夹的那些图像.

我已使用以下代码直接选择图库图像.

Intent intent = new Intent(Intent.ACTION_PICK);

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

startActivityForResult(intent, 1);
Run Code Online (Sandbox Code Playgroud)

上面的代码是从SD卡获取所有图像.但我只需要我的图片文件夹.我也尝试了Intent.ACTION_GET_CONTENT,结果相同.

请有人纠正我......

谢谢.

Bea*_*ear 0

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

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

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

一旦选择了照片,就会调用 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

获取路径方法

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);
}
Run Code Online (Sandbox Code Playgroud)

最后得到位图

public Bitmap getBitmap(String path, int size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = size;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

大小变量允许按一个因子缩放图像。如果您不想扩展,只需删除选项参数即可。

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

这也是一篇有用的文章以编程方式从 Android 的内置图库应用程序获取/选取图像