【问题标题】:How to get Galley Thumbnail for particular image folder in android?如何在 android 中获取特定图像文件夹的 Galley Thumbnail?
【发布时间】:2011-06-06 23:35:27
【问题描述】:

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

我已经使用下面的代码直接选择图库图片。

Intent intent = new Intent(Intent.ACTION_PICK);

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

startActivityForResult(intent, 1);

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

请任何人纠正我...

谢谢。

【问题讨论】:

  • 你得到答案了吗?如果有,请分享..

标签: android thumbnails gallery sd-card subdirectory


【解决方案1】:

这是我用来从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 变量允许按一个因子缩放图像。如果您不想缩放,只需删除 options 参数。

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

这也是一个有用的帖子Get/pick an image from Android's built-in Gallery app programmatically

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多