从画廊的Andr​​oid 6(棉花糖)图片 [英] Image from Gallery in Android 6(Marshmallow)

查看:539
本文介绍了从画廊的Andr​​oid 6(棉花糖)图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想从厨房接图像,从而该图像传递到服务器。

code是Android的5以下工作正常,但在Nexus 5的Andr​​oid 6我无法得到的图像信息。 日志跟踪我了

注意:code是Android的5及以下版本的做工精细

  12月11日至6日:27:43.736:W / System.err的(31678):java.lang.SecurityException异常:权限拒绝:阅读com.google.android.apps.photos。 contentprovider.MediaContentProvider URI content://com.google.android.apps.photos.contentprovider/0/1/content%3A//media/external/images/media/19138/ACTUAL/94710853从PID = 31678,UID = 10111要求提供出口,或grantUriPermission()
12月11日至6日:27:43.757:W / System.err的(31678):
在android.os.Parcel.readException(Parcel.java:1599)
12月11日至6日:27:43.757:W / System.err的(31678):
在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
12月11日至6日:27:43.757:W / System.err的(31678):
在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
12月11日至6日:27:43.757:W / System.err的(31678):
在android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
12月11日至6日:27:43.757:W / System.err的(31678):
在android.content.ContentResolver.query(ContentResolver.java:491)
12月11日至6日:27:43.757:W / System.err的(31678):
在android.content.ContentResolver.query(ContentResolver.java:434)
12月11日至6日:27:43.758:W / System.err的(31678):
在org.apache.cordova.file.ContentFilesystem.openCursorForURL(ContentFilesystem.java:258)
12月11日至6日:27:43.758:W / System.err的(31678):
在org.apache.cordova.file.ContentFilesystem.getFileMetadataForLocalURL(ContentFilesystem.java:169)
12月11日至6日:27:43.758:W / System.err的(31678):
在org.apache.cordova.file.FileUtils.getFileMetadata(FileUtils.java:822)
12月11日至6日:27:43.758:W / System.err的(31678):
在org.apache.cordova.file.FileUtils.access $ 500(FileUtils.java:52)
12月11日至6日:27:43.758:W / System.err的(31678):
在org.apache.cordova.file.FileUtils $ 15.run(FileUtils.java:394)
12月11日至6日:27:43.758:W / System.err的(31678):
在org.apache.cordova.file.FileUtils $ 25.run(FileUtils.java:551)
12月11日至6日:27:43.758:W / System.err的(31678):
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
12月11日至6日:27:43.758:W / System.err的(31678):
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:588)
12月11日至6日:27:43.758:W / System.err的(31678):
在java.lang.Thread.run(Thread.java:818)


解决方案

我相信你会需要请求的权限给用户在运行时(这是从API 22日至23最显着的变化之一)。

您可以试试这个片段中,从 Android开发者许可页 <提取/ p>

 如果(Build.VERSION.SDK_INT&GT; = 23){
//这里,thisActivity是当前活动
    如果(ContextCompat.checkSelfPermission(thisActivity,
                    Manifest.permission.READ_EXTERNAL_STORAG​​E)
            != PackageManager.PERMISSION_GRANTED){        //我们应该显示一个解释?
        如果(ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.READ_EXTERNAL_STORAG​​E)){            //显示一个expanation用户* *异步 - 不阻止
            //这个线程等待用户的响应!后用户
            //看到的解释,再次尝试请求的权限。        }其他{            //没有解释需要,我们可以要求的权限。            ActivityCompat.requestPermissions(thisActivity,
                    新的String [] {} Manifest.permission.READ_EXTERNAL_STORAG​​E,
                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAG​​E);            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAG​​E是
            //应用程序定义的int常量。这个回调方法获取
            //请求的结果。
        }
    }
  }

让我知道,如果它帮助。

In My Application I am trying to Pick image from galley, so as to pass that image to server.

Code is working fine on Android 5 and below, but for Android 6 on Nexus 5 I am not able to get image information. Log trace which I got

Note: Code is working fine on Android 5 and below versions

11-06 12:27:43.736: W/System.err(31678): java.lang.SecurityException: Permission Denial: reading com.google.android.apps.photos.contentprovider.MediaContentProvider uri content://com.google.android.apps.photos.contentprovider/0/1/content%3A//media/external/images/media/19138/ACTUAL/94710853 from pid=31678, uid=10111 requires the provider be exported, or grantUriPermission()
11-06 12:27:43.757: W/System.err(31678): 
at android.os.Parcel.readException(Parcel.java:1599)
11-06 12:27:43.757: W/System.err(31678): 
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
11-06 12:27:43.757: W/System.err(31678): 
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
11-06 12:27:43.757: W/System.err(31678): 
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
11-06 12:27:43.757: W/System.err(31678): 
at android.content.ContentResolver.query(ContentResolver.java:491)
11-06 12:27:43.757: W/System.err(31678): 
at android.content.ContentResolver.query(ContentResolver.java:434)
11-06 12:27:43.758: W/System.err(31678): 
at org.apache.cordova.file.ContentFilesystem.openCursorForURL(ContentFilesystem.java:258)
11-06 12:27:43.758: W/System.err(31678): 
at org.apache.cordova.file.ContentFilesystem.getFileMetadataForLocalURL(ContentFilesystem.java:169)
11-06 12:27:43.758: W/System.err(31678): 
at org.apache.cordova.file.FileUtils.getFileMetadata(FileUtils.java:822)
11-06 12:27:43.758: W/System.err(31678): 
at org.apache.cordova.file.FileUtils.access$500(FileUtils.java:52)
11-06 12:27:43.758: W/System.err(31678): 
at org.apache.cordova.file.FileUtils$15.run(FileUtils.java:394)
11-06 12:27:43.758: W/System.err(31678): 
at org.apache.cordova.file.FileUtils$25.run(FileUtils.java:551)
11-06 12:27:43.758: W/System.err(31678): 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-06 12:27:43.758: W/System.err(31678): 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-06 12:27:43.758: W/System.err(31678): 
at java.lang.Thread.run(Thread.java:818)

解决方案

I believe you'll need to request the permissions to the user at runtime (this is one of the most noticeable changes from api 22 to 23).

You can try this snippet, extracted from the Android Developers permission page

if (Build.VERSION.SDK_INT >= 23){    
// Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
                    Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
  }

Let me know if it helped.

这篇关于从画廊的Andr​​oid 6(棉花糖)图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆