【问题标题】:Choose image from image and get the directory returned从图像中选择图像并获取返回的目录
【发布时间】:2011-07-23 14:16:02
【问题描述】:

我必须启动 Android 的图片库并让用户选择图片。所以我希望在选择之后,返回目录图像。

我该怎么做?

谢谢。

【问题讨论】:

    标签: android image directory gallery


    【解决方案1】:

    米歇尔,

    要启动Intent 以从图库中选择图像,请使用以下代码:

    public void imageFromGallery() {
        Intent getImageFromGalleryIntent = 
          new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE);
    }
    

    然后,一旦用户做出选择,您就会在onActivityResult() 中得到结果,如下所示:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                switch(requestCode) {
                case SELECT_IMAGE:
                    String imagePath = getPath(data.getData());
                    break;
            }
        }
    
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        startManagingCursor(cursor);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    getPath() 是一个从返回的 URI 对象中获取路径的函数。这将返回带有您需要的路径的String

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2014-11-03
      • 2014-07-09
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多