Before You flag question as duplicate plz read the question...
so i am trying to select multiple image from gallery using Intent.when i select more than 1 image it works proper and when i select only 1 image it generate this error.i looked for like every NullPointer Exception question but no success.
FATAL EXCEPTION: main
Process: com.blackhat.applocker, PID: 1892
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:10227 flg=0x1 }} to activity {com.blackhat.applocker/com.blackhat.applocker.ProtactGallery}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3839)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3882)
at android.app.ActivityThread.access$1300(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference
at com.blackhat.applocker.ProtactGallery.onActivityResult(ProtactGallery.java:63)
at android.app.Activity.dispatchActivityResult(Activity.java:6302)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3835)
here is the code i am using for Intent
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 2);
And here is my ActivityResult Method content for images
if(requestCode == 2){
if(data.getClipData().getItemCount() == 1){
Toast.makeText(this, "Only 1", Toast.LENGTH_SHORT).show();
}else{
for(int i=0;i<data.getClipData().getItemCount();i++){
path.add(data.getClipData().getItemAt(i).getUri());
Log.d("RjList",path.get(i).toString());
Random rn=new Random();
if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}
}
}
it works proper with multiple image.
Why it's not working when i select only 1 image ?? Any Suggestion would be appriciated....
I believe ClipData is only returned when there are multiple Uris to send back. When there is just one you can get it with getData. You could do something like this to deal with that:
List<Uri> uris = new ArrayList<>();
ClipData cd = data.getClipData();
if ( cd == null ) {
Uri uri = data.getData();
uris.add(uri);
}
else {
for (int i = 0; i < cd.getItemCount(); i++) {
ClipData.Item item = cd.getItemAt(i);
Uri uri = item.getUri();
uris.add(uri);
}
}
From Tyler's Code suggestion you can use getData() method for one image but still it will generate error. so all you have to do is check whether data.getData() is null or not at time you check for request code like this
if(requestCode == 2 && data.getData() !=null){
if(cd == null){
path.add(data.getData());
Random rn = new Random();
if(copyFileFromUri(this,data.getData(),String.valueOf(rn.nextInt(500)))){
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}else{
for(int i=0;i<data.getClipData().getItemCount();i++){
path.add(data.getClipData().getItemAt(i).getUri());
Log.d("RjList",path.get(i).toString());
Random rn=new Random();
if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With