0

Below code is not working in Redmi 4A...it's working maximum devices it not working only Redmi 4A..i donot know it's throwing Nullpointer exception

Error:

Exeception: something went wrongjava.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference

  if (Build.VERSION.SDK_INT < 19) {
            Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            galleryIntent.setType("video/*, image/*");  // galleryIntent.setType("image/* video/*");
            galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            startActivityForResult(Intent.createChooser(galleryIntent,"Select Picture"), PICK_IMAGE_MULTIPLE);
        } else {
            Toast.makeText(PreviewImages.this,"WEdfsdfsdfdsfsfdsf",Toast.LENGTH_SHORT).show();
            Intent galleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
            galleryIntent.setType("*/*");
            galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
            galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            startActivityForResult(Intent.createChooser(galleryIntent,"Select Picture"), PICK_IMAGE_MULTIPLE);
        }

Here is OnActivityResult

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            // When an Image is picked
            if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data
                Log.e("previewimages"," **onActivityResult   ");
                String[] filePathColumn = {     MediaStore.Images.Media.DATA };

                    if(data.getData()!=null){
                    Log.e("previewimages"," **single  image selections   ");
                    String[] files = new String[1];
                    Log.e("printuri result","****oneimg    "+data.getData());
                    String  realPath = ImageFilePath.getPath(PreviewImages.this, data.getData());
                    files[0]  = realPath;

                    ImagepathModel imagepathModel = new ImagepathModel();
                    imagepathModel.setFilepath(realPath);
                    arrayList_copyfilepath.add(imagepathModel);

                    selectedimag_adapter.notifyDataSetChanged();
                    preview_playimagee.setEnabled(true);
                    preview_playimagee.setVisibility(View.VISIBLE);
                    displayFirstImages();
                    removepos =0;
                    if (myAnim !=null)
                        tv_add_sticker.startAnimation(myAnim);

                } else {
                    Log.e("previewimages"," **mutiple image selections   "+data.getClipData() +"   ==  "+data.getClipData().getItemCount());
                    if (data.getClipData() != null) {
                        ClipData mClipData = data.getClipData();
                        String[] files = new String[ mClipData.getItemCount()];
                        for (int i = 0; i < mClipData.getItemCount(); i++) {

                            ClipData.Item item = mClipData.getItemAt(i);
                            String  realPath = ImageFilePath.getPath(PreviewImages.this, item.getUri());

                            ImagepathModel imagepathModel = new ImagepathModel();
                            imagepathModel.setFilepath(realPath);
                            arrayList_copyfilepath.add(imagepathModel);

                            final File file = new File(realPath);
                            if (file.exists()) {

                                Log.e("fileexistes","*****          "+  "   File is existss      "  + realPath);
                            }else {
                                Log.e("fileexistes","*****          "+  "   Nofilefounds      ");

                            }

                           // preview_playimagee.setImageURI(data.getData());
                            Log.e("filepaths","****uris    "+item.getUri());
                            Log.e("filepaths","multipleimages          "+"    realpath \n "+realPath);


                        }


                         preview_playimagee.setEnabled(true);
                         preview_playimagee.setVisibility(View.VISIBLE);
                         selectedimag_adapter.notifyDataSetChanged();
                         displayFirstImages();
                         removepos = 0;
                        if (myAnim !=null)
                         tv_add_sticker.startAnimation(myAnim);
                     }
                }
            } else {
                if (arrayList_copyfilepath.size() == 0)
                Toast.makeText(this, "Please Select Atleast One Pick",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
            Log.e("Exeception","something went wrong"+e.toString());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • https://github.com/jkwiecien/EasyImage/issues/112 – IntelliJ Amiya Apr 30 '18 at 09:53
  • 2
    @IntelliJAmiya thank you..but i don't want use any lib sir becase max device it's working .. – Gowthaman M Apr 30 '18 at 09:57
  • 2
    @GowthamanM My app is also crashing when I picked an image from Redmi but it works in almost device. still, I did not get any solution, But maybe when I picked an image from Redmin, the gallery always ask "Ok" after choosing images from Redmi but other device did not ask when you have chosen image, and another problem is when you choose an image from Motorola device, then the app will also crash their because it does not have default file manager. – Farhana Naaz Ansari Apr 30 '18 at 10:06
  • 2
    @Selvin this is different problem, this is not a simple `NullPointerException`. – Farhana Naaz Ansari Apr 30 '18 at 10:07
  • 1
    @Selvin please read my question completely...it's working all the devices...i donot what is the cause getting null pointer exception...i know how to fix nullpointer exception – Gowthaman M Apr 30 '18 at 10:07
  • Lol, yes, it is simple NPE problem ... ClipData object is null ... the line is in the exception itself ... – Selvin Apr 30 '18 at 10:08
  • 2
    @selvin no, I also have been facing the same problem for a month, I could not find right solution regarding this. – Farhana Naaz Ansari Apr 30 '18 at 10:09
  • again yes, it is ... it's from his log statment ... – Selvin Apr 30 '18 at 10:10
  • 2
    @farhana thank you for your valuable response..i checked motorola it's wrking fine..i facing problem in redmi 4A only sir – Gowthaman M Apr 30 '18 at 10:10
  • 1
    @Selvin we want to know why only one device creating the same problem where almost devices do not create `nullpointer`, If you have any suggestion regarding this, Please let us know. – Farhana Naaz Ansari Apr 30 '18 at 10:14
  • 1
    @Selvin ok fine..do have any idea how to fix.i selecting the image even it's throws null pointer exception.. – Gowthaman M Apr 30 '18 at 10:14
  • It is all in the duplicated question ... – Selvin Apr 30 '18 at 10:15
  • 2
    @Selvin we know this is `NullPointerException` and `ClipData` is null, but why Intent is returning null in `redmi`. – Farhana Naaz Ansari Apr 30 '18 at 10:18
  • @farhana is there any workaround solution..how to overcome this please tell me sir – Gowthaman M Apr 30 '18 at 10:26
  • obviosuly because redmi's gallery doesn't implement it – Selvin Apr 30 '18 at 10:26
  • 1
    @Selvin can you brief it more. – Farhana Naaz Ansari Apr 30 '18 at 10:27
  • 2
    Xaoim modify the source of (or make custom) gallery which doesn't support it ... you have to live with that ... and do a null check ... you may send comunicate to the user to install other galley like google's photo to make your app works on redmi devices – Selvin Apr 30 '18 at 10:29
  • 1
    @Selvin it means no Solution regarding this. Okay, For example somewhere I saw the code where the data is picked individually for the specific device. It may work. – Farhana Naaz Ansari Apr 30 '18 at 10:31
  • There is a small chance that they are returning it not as `ClipData` but in some different way ... but it would involve to have redmi device and do returned Intent dump to find where Xaomi hide it ... other way is to fill some bug report to xaomi and pray for the fix – Selvin Apr 30 '18 at 10:34
  • @GowthamanM did you check [this](https://stackoverflow.com/questions/47977914/selecting-image-from-gallery-not-working-on-redmi-note-4) ? – Farhana Naaz Ansari Apr 30 '18 at 10:36
  • @farhana no use.., its not working. – Gowthaman M Apr 30 '18 at 10:46
  • @GowthamanM did you use File Provider? – Farhana Naaz Ansari Apr 30 '18 at 10:51
  • @farhana File Provider it's working well all the devices no issuse in that..using this https://developer.android.com/training/sharing/receive – Gowthaman M Apr 30 '18 at 10:53

0 Answers0