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
This is the simple method that i have created with other examples
call CHECKGALLERYPERMISSION() method where you want and paste the code inside method
private void CHECKGALLERYPERMISSION()
{
int MY_READ_PERMISSION_REQUEST_CODE =1 ;
int PICK_IMAGE_REQUEST = 2;
if (ContextCompat.checkSelfPermission(YourActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
else
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
MY_READ_PERMISSION_REQUEST_CODE);
}
else
{
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
if (requestCode == MY_READ_PERMISSION_REQUEST_CODE
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null)
{
Uri uri = data.getData();
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(YourActivity.this.getContentResolver(), uri);
imageview.setImageBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}