Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share file uri from ACTION_PICK

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.

like image 976
Nanoc Avatar asked Jul 06 '18 10:07

Nanoc


3 Answers

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.

like image 183
Nanoc Avatar answered Oct 13 '22 03:10

Nanoc


This snippet code works correctly in the emulator and real device

    private val FILE_SELECT_CODE = 101
    private val whatsAppPackage = "com.whatsapp"

       fun sharePicture() {
         val intent = Intent(Intent.ACTION_GET_CONTENT)
         intent.type = "image/*"
         intent.addCategory(Intent.CATEGORY_OPENABLE)
         if (intent.resolveActivity(packageManager) != null){
         startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE)
         }else{
             Toast.makeText(this, "Please install a File Manager.", 
             Toast.LENGTH_SHORT).show()
         }
      }


      override fun onActivityResult(requestCode: Int, resultCode: Int,                                       
      data: Intent?) {
         super.onActivityResult(requestCode, resultCode, data)

         if (resultCode == Activity.RESULT_OK) {
             when (requestCode) {
                 FILE_SELECT_CODE -> {
                        val sendIntent = Intent()
                        sendIntent.action = Intent.ACTION_SEND
                        sendIntent.putExtra(Intent.EXTRA_TEXT, "Some text")
                        sendIntent.putExtra(Intent.EXTRA_STREAM, data!!.data)
                        sendIntent.type = "image/*"
                        sendIntent.addFlags(
                                            Intent.FLAG_GRANT_READ_URI_PERMISSION)
                        sendIntent.`package` = whatsAppPackage
                     if (sendIntent.resolveActivity(packageManager) != null) {
                        startActivity(sendIntent)
                     } else {
                          goToGooglePlay(whatsAppPackage)
                     }
               }
            }
         } 
      }


       fun goToGooglePlay(appPackageName: String) {
         val playIntent = Intent(Intent.ACTION_VIEW, 
                          Uri.parse("market://details?id=$appPackageName"))

         if (playIntent.resolveActivity(packageManager) != null){
             startActivity(playIntent)
         }else
             startActivity(Intent(Intent.ACTION_VIEW, 
                        Uri.parse("https://play.google.com/store/apps/details? id=$appPackageName")))
        }


       fun isPackageExisted(packageName: String): Boolean {
           val pm = packageManager
           val isInstalled: Boolean
           isInstalled = try {
               pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
               true
           } catch (e: PackageManager.NameNotFoundException) {
               false
           }

          return isInstalled
       }
like image 33
Ali Rezaiyan Avatar answered Oct 13 '22 01:10

Ali Rezaiyan


As log of your code says you can't cast uri to String Arraylist.

so try converting StringURI to ArrayListURI like so

ArrayList<Uri> tempURI = new ArrayList<>();
tempURI.add(imageUri); //Your String URI

And Now Pass tempURI with intent.

sendIntent.putExtra(Intent.EXTRA_STREAM, tempURI);

Try this and check whether it's working on Simulator or not..

Also read this

ACTION_SEND supports EXTRA_STREAM, but only for a single Uri. ACTION_SEND_MULTIPLE supports EXTRA_STREAM with an ArrayList<Uri>.

like image 26
Rj_Innocent_Coder Avatar answered Oct 13 '22 03:10

Rj_Innocent_Coder