如何获取 Android 中特定图像文件夹的厨房缩略图?

发布于 2024-10-11 05:40:07 字数 363 浏览 4 评论 0原文

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

我使用下面的代码直接选择图库图像。

Intent intent = new Intent(Intent.ACTION_PICK);

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

startActivityForResult(intent, 1);

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

请任何人纠正我...

谢谢。

I have some saved images at Pictures folder in SD Card. I want to access those images of my folder directly.

I have used the below code to pick Gallery images directly.

Intent intent = new Intent(Intent.ACTION_PICK);

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

startActivityForResult(intent, 1);

The above code is getting all images from the SD Card. But i need only of my Pictures folder. I also tried Intent.ACTION_GET_CONTENT, the same result.

Please anybody correct me...

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2024-10-18 05:40:07

这是我用来从 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);
    }
}

get Path 方法

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);
}

最后获取 Bitmap

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 的内置图库应用程序以编程方式

This is the code I use to select a picture from the sd card

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

Note this loads the root folder.

Once a photo is selected the onActivityResult method is called an the image can be gotten.

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);
    }
}

The get Path method

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);
}

And finally to get the Bitmap

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

The size variable allows the scaling of the image by a factor. If you don't wish to scale simply remove the options parameter.

I'm not sure how to tell it to pick from another folder other than the root.

Here is a helpful post too Get/pick an image from Android's built-in Gallery app programmatically

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文