首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式从Android的内置库应用程序中获取/选择图像

问以编程方式从Android的内置库应用程序中获取/选择图像
EN

Stack Overflow用户
提问于 2010-01-31 06:11:43
回答 19查看 334.6K关注 0票数 278

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

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

你有什么意见建议?

EN

回答 19

Stack Overflow用户

回答已采纳

发布于 2010-04-14 18:21:34

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

更新

我刚刚检查了我的原始答案,并创建了一个简单的Android Studio项目,您可以从github签出并直接导入到您的系统中。

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

(请注意,多个文件的选择仍然需要工作)

单张图片选择

多亏了用户mad,它支持文件资源管理器中的图像。

代码语言:javascript
复制
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:

代码语言:javascript
复制
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

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

代码语言:javascript
复制
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
       }
   }
} 

请注意,只有接口层18+才支持此功能。

票数 356
EN

Stack Overflow用户

发布于 2010-12-17 19:37:05

以下是hcpl发布的优秀代码的更新。但这也适用于OI文件管理器,astro文件管理器和媒体集(测试)。所以我猜它适用于每个文件管理器(除了上面提到的文件管理器之外,还有其他文件管理器吗?)对他写的代码做了一些修改。

代码语言:javascript
复制
public class BrowsePicture extends Activity {

    //YOU CAN EDIT THIS TO WHATEVER YOU WANT
    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    //ADDED
    private String filemanagerstring;

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

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

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

                //OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();

                //MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);

                //DEBUG PURPOSE - you can delete this if you want
                if(selectedImagePath!=null)
                    System.out.println(selectedImagePath);
                else System.out.println("selectedImagePath is null");
                if(filemanagerstring!=null)
                    System.out.println(filemanagerstring);
                else System.out.println("filemanagerstring is null");

                //NOW WE HAVE OUR WANTED STRING
                if(selectedImagePath!=null)
                    System.out.println("selectedImagePath is the right one for you!");
                else
                    System.out.println("filemanagerstring is the right one for you!");
            }
        }
    }

    //UPDATED!
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }
票数 134
EN

Stack Overflow用户

发布于 2014-02-03 06:19:18

hcpl的方法在KitKat之前工作得很好,但不能与DocumentsProvider API一起工作。要做到这一点,只需遵循文档提供商的官方Android教程:https://developer.android.com/guide/topics/providers/document-provider.html ->打开文档,位图部分。

我简单地使用了hcpl的代码并对其进行了扩展:如果包含图像的检索路径的文件抛出异常,我将调用此函数:

代码语言:javascript
复制
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
        ParcelFileDescriptor parcelFileDescriptor =
             getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
}

已在Nexus 5上测试。

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2169649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档