android - 以编程方式从 Android 的内置 Gallery 应用程序中获取/选择图像

标签 android image android-intent gallery

我正在尝试从我的应用程序内部打开 Gallery 内置应用程序中的图像/图片。

我有图片的 URI(图片位于 SD 卡上)。

你有什么建议吗?

最佳答案

这是一个完整的解决方案。我刚刚使用@mad 在下面的答案中提供的信息更新了这个示例代码。另请查看@Khobaib 提供的以下解决方案,解释如何处理 picasa 图像。

更新

我刚刚回顾了我的原始答案,并创建了一个简单的 Android Studio 项目,您可以从 github 中 check out 并直接导入您的系统。

https://github.com/hanscappelle/SO-2169649

(注意多文件选择仍然需要工作)

单张图片选择

感谢用户 mad,支持来自文件浏览器的图像。

public class BrowsePictureActivity extends Activity {

    // this is the action code we use in our intent, 
    // this way we know we're looking at the response from our own action
    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.Button01)
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                        // in onCreate or any event where your want the user to
                        // select a file
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);
                    }
                });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
            }
        }
    }

    /**
     * helper to retrieve the path of an image URI
     */
    public String getPath(Uri uri) {
            // just some safety built in 
            if( uri == null ) {
                // TODO perform some logging or show user feedback
                return null;
            }
            // try to retrieve the image from the media store first
            // this will only work for images selected from gallery
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if( cursor != null ){
                int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String path = cursor.getString(column_index);
                cursor.close();
                return path;
            }
            // this is our fallback here
            return uri.getPath();
    }

}

选择多张图片

由于有人在评论中要求提供该信息,因此最好收集信息。

在 Intent 上设置一个额外的参数EXTRA_ALLOW_MULTIPLE:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

并在结果处理中检查该参数:

if (Intent.ACTION_SEND_MULTIPLE.equals(data.getAction()))
        && Intent.hasExtra(Intent.EXTRA_STREAM)) {
    // retrieve a collection of selected images
    ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    // iterate over these images
    if( list != null ) {
       for (Parcelable parcel : list) {
         Uri uri = (Uri) parcel;
         // TODO handle the images one by one here
       }
   }
} 

请注意,这仅受 API 级别 18+ 支持。

关于android - 以编程方式从 Android 的内置 Gallery 应用程序中获取/选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10452051/

相关文章:

android - 没有默认启动的 Intent ACTION_SEND

android - Firebase 适合做实时 table 游后端吗?

java - 如何在android中获取图像的(x,y)坐标?

Java FXML : Why is my imageView not Showing, 以及如何从我的计算机上传图像?

c++ - 如何将 QLabel 的大小设置为其像素图中图像的大小?

java - Android 通过单击不适用于 Galaxy 5 的按钮调用电话

java - 是否可以仅更改 TextView 的一部分?

javascript - 如何更改 Android WebView 中的 iframe 大小?

c# - 如何以编程方式设置图像源

android - 无需等待 Intent 通知即可获取屏幕/电话状态