【问题标题】:Share image is not working via ACTION.SEND无法通过 ACTION.SEND 共享图像
【发布时间】:2014-07-12 05:23:06
【问题描述】:

我正在使用 ACTION_SEND 在 facebook 上分享图片,但它不起作用。代码在这里

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”)

【问题讨论】:

  • 尝试使用 .PNG 图像而不是 .JPG 图像。你有吗?

标签: android facebook android-intent


【解决方案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 "";
}

【讨论】:

  • 您在分享对话框中看到照片了吗?
  • 是的,我有手表,也可以写文字,但在按下发布按钮后。它在窗口中给出错误
  • 那么它就可以工作了,当我像上面的代码一样共享图像时,我编辑 ans 并放置屏幕。
  • 我可以直接从服务器共享图像还是首先我必须保存在 sdcard 中..??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多