码迷,mamicode.com
首页 > 移动开发 > 详细

Android从图库选择照片

时间:2016-04-01 21:50:05      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

从机里取照片,开头用网上找的代码测试,导致类似下面这样的Crash:
java.lang.RuntimeException: Failure delivering result
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/1909 from pid=29038, uid=10236
requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()


好在在这里找到解决了
http://developer.android.com/samples/StorageClient/src/com.example.android.storageclient/StorageClientFragment.html#l233

用里面的getBitmapFromUri方法就行:

        /** Create a Bitmap from the URI for that image and return it.
         *
         * @param uri the Uri for the image to return.
         */
        private Bitmap getBitmapFromUri(Uri uri) {
            ParcelFileDescriptor parcelFileDescriptor = null;
            try {
                parcelFileDescriptor =
                        getActivity().getContentResolver().openFileDescriptor(uri, "r");
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                parcelFileDescriptor.close();
                return image;
            } catch (Exception e) {
                Log.e(TAG, "Failed to load image.", e);
                return null;
            } finally {
                try {
                    if (parcelFileDescriptor != null) {
                        parcelFileDescriptor.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "Error closing ParcelFile Descriptor");
                }
            }
        }

 

Android从图库选择照片

标签:

原文地址:http://www.cnblogs.com/zhanglianzhong/p/5346412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!