Share image is not working via ACTION.SEND

2019-08-29 11:48发布

问题:

I am sharing image on facebook using ACTION_SEND, but its not working .code is here

try {
                File myFile = new File("/mnt/sdcard/DCIM/100MEDIA/aa.jpg");
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                String ext = myFile.getName().substring(
                        myFile.getName().lastIndexOf(".") + 1);
                String type = mime.getMimeTypeFromExtension(ext);
                Intent sharingIntent = new Intent(
                        "android.intent.action.SEND");
                sharingIntent.setType(type);
                sharingIntent.putExtra("android.intent.extra.STREAM",
                        Uri.fromFile(myFile));
                startActivity(Intent.createChooser(sharingIntent,
                        "Share using"));
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }

but it gives the error "Cannot upload"

I am using this link ("Share Image via android app using ACTION_SEND not working")

回答1:

Try this way,hope this will help you to solve your problem.

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   try {
       File myFile = new File(Environment.getExternalStorageDirectory()+"/100MEDIA/aa.jpg");
       Intent sharingIntent = new Intent("android.intent.action.SEND");
       sharingIntent.setType(getMimeType(myFile.getPath()));
       sharingIntent.putExtra("android.intent.extra.STREAM",Uri.fromFile(myFile));
       startActivity(Intent.createChooser(sharingIntent,"Share using"));
   } catch (Exception e) {
      Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
   }
}

public String getMimeType(String filePath) {
   String type = null;
   String extension = getFileExtensionFromUrl(filePath);
   if (extension != null) {
       MimeTypeMap mime = MimeTypeMap.getSingleton();
       type = mime.getMimeTypeFromExtension(extension);
   }
   return type;
}

public String getFileExtensionFromUrl(String url) {
    int dotPos = url.lastIndexOf('.');
    if (0 <= dotPos) {
        return (url.substring(dotPos + 1)).toLowerCase();
    }
    return "";
}