2

I am trying to pick the image from the gallery and it is working fine in below android 7.1 but from android 7.1 it is not working for me. How to fix the same. This the code which I tried :

public void LaunchGalleryView() {
        mFileUri = null;
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE_GALLERY);
    }

This is the code in onActivityResult():

 case REQUEST_CODE_GALLERY:
                if (data == null) {
                    return;
                }
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                File file = new File(picturePath);
                mFileUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
                performCrop();
                break;

How to fix the same please help me.

This is the error log:

12-28 05:13:30.085 13533-13533/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.welldoc.platform.android.cyan, PID: 13533 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/local%3Aefc38517-5fd9-4b57-afbf-6329e508fb66/ORIGINAL/NONE/1646850308 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253Aefc38517-5fd9-4b57-afbf-6329e508fb66/ORIGINAL/NONE/1646850308} }} to activity {com.welldoc.platform.android.cyan/com.welldoc.platform.android.ui.profile.ProfileDetailsEditActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:4324) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367) at android.app.ActivityThread.-wrap19(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException at java.io.File.(File.java:282) at com.welldoc.platform.android.ui.profile.ProfileDetailsEditActivity.onActivityResult(ProfileDetailsEditActivity.java:1059) at android.app.Activity.dispatchActivityResult(Activity.java:7235) at android.app.ActivityThread.deliverResults(ActivityThread.java:4320) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)  at android.app.ActivityThread.-wrap19(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
shree
  • 158
  • 2
  • 11

4 Answers4

0

1. Compare the picture path with other mobiles picture selected path, sometimes the path format may vary, so you can get null.

Viveka N
  • 1
  • 1
  • so, how we can set the format differently based upon the version? currently for the devices above 7.1 I am getting piicturepath null I am not getting any picturepath itself. – shree Dec 28 '17 at 10:29
  • are you compared the selected path? do you find any differences between them? – Viveka N Dec 29 '17 at 12:10
  • ye I got the difference. for few devices path starts with file:/// and for few devices path starts with content:///. the device whose path has been started with file:/// is crashing – shree Jan 02 '18 at 05:19
  • refer this link may be useful for you: http://codetheory.in/android-pick-select-image-from-gallery-with-intents/ – Viveka N Jan 04 '18 at 10:34
0

The second argument of the method FileProvider.getUriForFile() requires you to create custom FileProvider and define it in app's manifest:

authority String: The authority of a FileProvider defined in a element in your app's manifest.

If you just want to get Uri for your file, you can use the next code:

Uri.fromFile(file);
Denysole
  • 3,903
  • 1
  • 20
  • 28
0

I am trying to pick the image from the gallery and it is working fine in below android 7.1

Only on the devices that you tried, for the scenarios that you tried. It will fail on many other devices, and for other scenarios:

  • There is no requirement for ACTION_PICK to return a Uri from the MediaStore
  • There is no requirement for the MediaStore to give you a path
  • There is no requirement for the MediaStore to give you a path that you can read from, let alone write to

This is the code in onActivityResult():

You already have a Uri. You do not need FileProvider for that. You do not need to query the MediaStore for that. selectedImage is the Uri. So, use that with your preferred image cropping library, and have that library save its results to some file that you control.

If, for some reason, you need a file at the outset:

  • Call getContentResolver() to get a ContentResolver
  • Call openInputStream() on the ContentResolver, passing in selectedImage, to get an InputStream on that content
  • Create a FileOutputStream on a file that you control (e.g., in getCacheDir())
  • Copy the data from the InputStream to the FileOutputStream
  • Use the resulting file
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Try this

  • you have to remove this line from your code just add this startActivityForResult(intent,1);
  • I think that you are passing wrong intent which tends not to open gallery it open DocumentProvider
  • put this in your OnActivityResult

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
         if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
            try {
                uri = null;
                uri = data.getData();
                Bitmap bmp = null;
                file_galleryimagepath = null;
                bmp = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
    
                File f = new File(getActivity().getCacheDir(), System.currentTimeMillis() + ".jpg");
                if (f.exists())
                    f.delete();
                f.createNewFile();
                Bitmap bitmap = bmp;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
                byte[] bitmapdata = bos.toByteArray();
                FileOutputStream fos = new FileOutputStream(f);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
                file_galleryimagepath = f;
                Bitmap map = BitmapFactory.decodeFile(file_galleryimagepath.toString());
                iv_userImage.setImageBitmap(map);
    
            }catch (Exception e){
                Log.e("204","HFP>>>"+e+"<<catch");
            }
        }
      }
    }
    

  • when you try to get file path from DocumentProvider' its result becomescrash` so you've to pass intent like this.
Vishal Yadav
  • 1,020
  • 4
  • 15
  • 30