Image from Gallery in Android 6(Marshmallow)

后端 未结 5 1793
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 16:39

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条回答
  •  佛祖请我去吃肉
    2020-12-05 16:54

    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.

提交回复
热议问题