Arjun Singh
Arjun Singh

Reputation: 724

Unable to send images to other apps, tried hike, Messenger and Whatsapp

I am trying to share images from my app to other app (Hike, Facebook, Messenger etc) but every time I am getting different error. Gone through almost every Q&A but problem not solved yet.

This is my code:

                       filepath = Environment.getExternalStorageDirectory();
                       cacheDir = new File(filepath.getAbsolutePath()
                       + "/LikeIT/");
                       cacheDir.mkdirs();
                       Intent intent = new Intent();
                       intent.setType("image/jpeg");
                       intent.setAction(Intent.ACTION_SEND);

                       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
                                       .getAbsolutePath())));
                       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                       activity.startActivity(intent);

I have changed the below line many times, but didn't get the solution:

    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
                                       .getAbsolutePath())));       

I have changed this with:

  1.  intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse(str1));
  2.  intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file1);

but didn't get as needed. And also when i am sending image to Whatsapp it is not showing image, and after sending it is showing.

enter image description here

Upvotes: 0

Views: 1124

Answers (2)

OstachioCaro
OstachioCaro

Reputation: 1

TL;DR ::: You need to enable read/write permissions for WhatsAPP (or any other APP) to use the images. Create a temporary file --- OR --- make your APP save files to external storage.

This is exactly what happened to me and my app. Took me a day of fiddling around, and I've finally resolved it.

No matter what, this line of code did not work (or anything relating to modifying the Uri object ) : intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Nor the following from FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE); then changing Context.MODE_PRIVATE to Context.MODE_WORLD_READABLE ... which was deprecated after API-14

It's not anything wrong with this part:

 filepath = Environment.getExternalStorageDirectory();
                       cacheDir = new File(filepath.getAbsolutePath()
                       + "/LikeIT/");
                       cacheDir.mkdirs();
                       Intent intent = new Intent();
                       intent.setType("image/jpeg");
                       intent.setAction(Intent.ACTION_SEND);

                       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
                                       .getAbsolutePath())));

That part is working just fine, since it starts the WhatsApp process --- and waits for you to add a caption. By adding intent.putExtra(Intent.EXTRA_TEXT, text_string); it'll take whatever string values found in text_string and adds it as the pictures caption. Unfortunately, intent.putExtra() is received/understood by WhatsAPP with just one picture with one caption ... I'm not sure how to make it multiple pictures with multiple captions...

Another tip: In case the error output says File(s) Directory doesn't exist, it's likely due to the fact that the line mkdirs() did not work. The tutorial uses two chosen directories APP_PATH_SD_CARD and APP_THUMBNAIL_PATH_SD_CARD --- and asks mkdirs() to create both of them at the same time... which for some reason, mkdirs() does not like to do. So I asked mkdirs() to create them one at a time:

   File dir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD );
   if (!dir.exists()) { dir.mkdirs();}

  File dirHoldingImages = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD);
    if (!dirHoldingImages.exists()) { dirHoldingImages.mkdirs(); }

// carry-on with the rest of the saveFileToExternalStorage code

Hope those links helps others that stumble across the same issues. :)

Upvotes: -1

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

Try below code that works fine in my app

public void onShareItem(View v) {
        // Get access to bitmap image from view

        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(descpic);
        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            shareIntent.putExtra(Intent.EXTRA_TEXT, desc.getText().toString());
            shareIntent.setType("image/*");
            // Launch sharing dialog for image
            startActivity(Intent.createChooser(shareIntent, "Share Image"));
        } else {
            // ...sharing failed, handle error
            Log.e("check for intent", "Couldn't get anything");
        }
    }

    // Returns the URI path to the Bitmap displayed in specified ImageView
    public Uri getLocalBitmapUri(ImageView imageView) {
        imageView.buildDrawingCache();
        Bitmap bm = imageView.getDrawingCache();

        OutputStream fOut = null;
        Uri outputFileUri=null;
        try {
            File root = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "folder_name" + File.separator);
            root.mkdirs();
            File imageFile = new File(root, "myPicName.jpg");
            outputFileUri = Uri.fromFile(imageFile);
            fOut = new FileOutputStream(imageFile);
        } catch (Exception e) {
            Toast.makeText(this, "Error occured. Please try again later.", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        try {
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            return outputFileUri;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

Upvotes: 1

Related Questions