5

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

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

Intent intent = new Intent(Intent.ACTION_PICK);

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

startActivityForResult(intent, 1);

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

请任何人纠正我...

谢谢你。

4

1 回答 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 变量允许按一个因子缩放图像。如果您不想缩放,只需删除 options 参数。

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

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

于 2011-05-24T00:44:03.073 回答