im trying to make an app that will ask the user to pick a image file and then send it via intent to another app (Whatsapp in this case but this should work with other apps)
Im doing this to request the file:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
This returns a "content://" URI, but when sending it via intent:
if (isPackageExisted(whatsAppPackage)) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Some text");
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setPackage(whatsAppPackage);
startActivity(sendIntent);
}else{
goToGooglePlay(whatsAppPackage);
}
Whatsapp shows a toast saying the file format its not supported and i can see a stacktrace on the logcat
java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.util.ArrayList
at android.os.Bundle.getParcelableArrayList(Bundle.java:838)
at android.content.Intent.getParcelableArrayListExtra(Intent.java:5405)
at com.whatsapp.ContactPicker.c(ContactPicker.java:12)
at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:526)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
...
Im not sure if i can share the "picker app" uri with another app, something to do with permissions?
What would be the correct way of doing this? do i need to make a copy of the file so my app would be able to share it?
EDIT:
This is working with other apps like Gmail, but not in whatsapp so is possible to have a solution that works with all or most apps? i dont care if i have to make separate methods for sharing but whatsapp is a must.
The code is working fine on a real device, it's on the android simulator where it doesn't work.
I wasn't expecting it, i had tried in the phone just in case.