共享图像无法通过ACTION.SEND工作

时间:2014-07-12 05:23:07

标签: android facebook android-intent

我正在使用ACTION_SEND在Facebook上分享图片,但它无法使用.code就在这里

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();
            }

但它出现错误“无法上传”

我正在使用此链接(“Share Image via android app using ACTION_SEND not working”)

1 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

@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 "";
}

enter image description here enter image description here