2016-08-15 37 views
0

我有一个共享按钮,它将共享带有文本的drawable的图像。从Drawable获取PNG的URI返回没有任何文件扩展名的空白文件

public boolean shareGame(String msg) { 
    if (Configuration.Share_WITH_IMAGE) { 
     Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.setType("image/png"); 
     Uri uri = Uri 
       .parse("android.resource://" + getPackageName() + "/" + R.drawable.share_image); 

     shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
     shareIntent.putExtra(Intent.EXTRA_TEXT, msg + " " + GOOGLE_PLAY_URL); 
     startActivity(Intent.createChooser(shareIntent, "Share: " + msg)); 
    } else { 
     Intent sendIntent = new Intent(); 
     sendIntent.setAction(Intent.ACTION_SEND); 
     sendIntent.putExtra(Intent.EXTRA_TEXT, msg + GOOGLE_PLAY_URL); 
     sendIntent.setType("text/plain"); 
     startActivity(Intent.createChooser(sendIntent, msg)); 
    } 

    return true; 

} 

目前,当我尝试通过电子邮件分享时,它会返回一个没有任何扩展名的空文件。 WhatsApp共享给出了不支持的文件类型的错误。

我是否需要将其转换为位图?如果是这样,那么最好的办法是什么。

谢谢。

+1

'android.resource'是一个很少使用的'Uri'方案。某些应用程序可能不会期望它,并可能在使用它时遇到问题“我需要将其转换为位图吗?” - 不,因为“ACTION_SEND”不支持“位图”。您可能希望使用“ContentProvider”来提供图像。 – CommonsWare

回答

2

此代码的工作..我已经测试它Gmail和WhatsApp的

Bitmap icon = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_launcher); 
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/jpeg"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "share_image.jpg"); 

    try { 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    share.putExtra(Intent.EXTRA_TEXT, "send text"); 

    share.putExtra(Intent.EXTRA_STREAM, 
      Uri.parse("file:///sdcard/share_image.jpg")); 
    startActivity(Intent.createChooser(share, "Share Image")); 

编辑:我不知道为什么你所得到的错误希望你已经添加权限如果不添加以下权限

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

仍然无法使用。这是我在logcat中得到的。 W/ContextImpl:在没有合格用户的情况下调用系统进程中的方法:android.app.ContextImpl.sendBroadcast:830 – Tahir

+0

终于完成了。我不得不使用'Uri.parse(“file:/// storage/emulated/0 /share_image_new.jpg”));'不知道为什么,但是我已经更新了权限但sdcard不适用于我。没有办法直接从drawable访问图像,这样我就不必手动将图像保存在存储上了? – Tahir

-1
Uri imageUri = Uri.parse("android.resource://" + getPackageName() 
    + "/drawable/" + "ic_launcher"); 
Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); 
shareIntent.setType("image/jpeg"); 
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(Intent.createChooser(shareIntent, "send")); 

https://stackoverflow.com/a/20333318/2316935

+0

不幸的是,这不适用于我和我有同样的问题。 – Tahir

+0

不只是发布代码w/o解释。如果解释和代码来自另一个问题,那么你只需要在评论中链接到该问题。 –

相关问题