9

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.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • And if the user chooses an email app? – greenapps Jul 06 '18 at 10:54
  • @greenapps im targeting directly the whatsapp package, im not using a chooser and this is meant to work only with whatsapp – Nanoc Jul 06 '18 at 11:00
  • Does not matter. For a test make changes so the user can select an email app. – greenapps Jul 06 '18 at 11:02
  • @greenapps you must be right, with the gmail app the image is attached correctly but this is not working with whatsapp so it isn't a problem with permissions? – Nanoc Jul 06 '18 at 11:11
  • can you try share image only then share text I read but I am not sure that you can't share both text and image – androidAhmed Jul 11 '18 at 09:32
  • it doesn't look like file format not supported to me, the log say ClassCastException, possible that whatsapp is expecting ArrayList? – hjchin Jul 11 '18 at 09:39
  • i'll award the bounty to someone that can explain why this is not working on the simulator. – Nanoc Jul 11 '18 at 09:47

3 Answers3

4

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.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
1

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
       }
Ali Rezaiyan
  • 3,011
  • 3
  • 16
  • 27
  • In case to know that there is a package already exists, it is better to use " intent.resolveActivity(packageManager) " instead of " try-catch " – Ali Rezaiyan Jul 17 '18 at 04:45
  • this is the same code i was using, in the simulator whatsapp says the image format is not supported – Nanoc Jul 17 '18 at 14:39
1

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>.

Rohit Chauhan
  • 1,119
  • 1
  • 12
  • 30