0

I have application with images and I want share image which is choosen by user in some other application. From other question here I know that I must put the image in public place so it can be accesed by other application. But I still get error "no application can perform this action" any idea where am I doing mistake? Code for copying image to SD card:

String path = Environment.getExternalStorageDirectory().toString();
  File file = new File(path,String.valueOf(idOfImage));
      if (!file.exists()) 
           {
      Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),idOfImage);
      FileOutputStream out = null;
          try {
                 out = new FileOutputStream(file);
                 myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
              } catch (Exception e) 
                {
                   e.printStackTrace();
                } finally {
          try {
                 if (out != null) 
                {
                   out.close();
                }
                } catch (IOException e) 
                 {
                   e.printStackTrace();
                 }
            }

Code for sending the intent and picking chooser:

Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    Uri uri = Uri.fromFile(file);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);   
                      StartActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.share)));

Thanks for answers.

EDIT: Works fine when sharingIntent.setType("image/png"); lane added, with Gmail and G+ , but doesnt work with Messengers FB and others.

DavidM
  • 41
  • 6

2 Answers2

0
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

try this code

  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);

  shareIntent.setType("image/*");

  shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)                       
  v.getTag(R.string.app_name));

  shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // put your image URI

  PackageManager pm = v.getContext().getPackageManager();

   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);

   for (final ResolveInfo app : activityList) 
     {
     if ((app.activityInfo.name).contains("facebook")) 
     {

       final ActivityInfo activity = app.activityInfo;

       final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);

      shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);

      shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

      shareIntent.setComponent(name);

      v.getContext().startActivity(shareIntent);

      break;
    }
  }

Edit 1

Intent shareIntent = new Intent();
         shareIntent.setAction(Intent.ACTION_SEND);

         shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));  //optional//use this when you want to send an image
         shareIntent.setType("image/jpeg");
         shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
         startActivity(Intent.createChooser(shareIntent, "send"));

use this code i tried it and it works here at my side

Meenal
  • 2,879
  • 5
  • 19
  • 43
  • v. refers to View, right? I used your code and it open only FB but still without image:-/ – DavidM Oct 16 '14 at 11:43
  • For messengers :It says file is not supported. and for FB messenger: " Sorry, Messenger cant't handle this file type right now. – DavidM Oct 16 '14 at 12:31
  • which type of file you are using – Meenal Oct 16 '14 at 12:32
  • PNG but in future I would like to use gifs too but it seems impossible so far:D – DavidM Oct 16 '14 at 12:34
  • can you share your code so that i can try here and let u knw – Meenal Oct 16 '14 at 12:35
  • I simplified it a little and here is core of the problem. http://www.codeshare.io/8cPod – DavidM Oct 16 '14 at 12:57
  • Still same error:-/ "Sorry, Messenger cant't handle this file type right now." – DavidM Oct 16 '14 at 13:19
  • Nop. Just tried 50x50 pixel image 406 byte size. Could you share simple apk so I could try if problem is not on my side just? – DavidM Oct 16 '14 at 13:24
  • 1
    I stole some lines from your code and now it works for sms messenger and most of others :-) Only FB messenger now shows window "send separatly" but it did even when I tryed your app so I guess problem is on my side. Anyway big thanks. – DavidM Oct 16 '14 at 15:28