Sharing images in other application

谁都会走 提交于 2021-01-29 18:19:00

问题


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.


回答1:


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




回答2:


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"));


来源:https://stackoverflow.com/questions/26377483/sharing-images-in-other-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!