2012-12-20 98 views
9

我想分享图像中的图像view.but但我不想保存到SDcard。 但是当我使用意向分享我以前在SD卡android在ImageView中共享图像而不保存在SD卡

代码

Intent share = new Intent(Intent.ACTION_SEND); 
       share.setType("image/jpeg"); 
       share.putExtra(Intent.EXTRA_STREAM,Uri.parse(path)); 
       startActivity(Intent.createChooser(share, "Share Image")); 

此路径指定图像的位置,但我不想保存的图片有可能..

+0

这个Q&A是值得一读http://stackoverflow.com/questions/9049143/android-share-intent-for-a-bitmap-is-it-possible-not-to-保存之前共享 – Suragch

回答

1

您也可以将其作为媒体库内容提供者URI共享(如果图像已经在手机上,或者它来自网络,则可以共享该URL(虽然效果不一样)。

但是如果传出从网络直接解码到位图,现在想分享它作为一个适当的形象,是啊,你真的需要该文件!

+2

我无法相信有没有办法做到这一点,将图像保存到文件! – Charlires

+0

没有保存到SD卡这是可能的, –

5

我能够做到这一点:

File file = new File(getCacheDir(), "screenshot.png"); 
... 
file.setReadable(true, false); 
Uri uri = Uri.fromFile(file); 
... 
intent.putExtra(Intent.EXTRA_STREAM, uri); 

这样,我保存在我自己的缓存文件夹中的文件,所以我不与任何公共文件夹或愚弄取决于SD卡存在的。

此外,如果用户删除我的应用程序,它会自动删除,但我也使用startActivityForResult/onActivityResult删除该文件,并在完成共享时将其文件夹设回私人。

(我个人希望能找到一种方法来共享一个比特流,并完全避免创建文件的步骤,但我不认为这是可能的。)

[编辑:我发现这并未” t工作在2.3.6文件必须在文件上:/// mnt/sdcard。]

+0

你能够与gmail共享图像吗?当我尝试时,我收到了一些错误信息。 –

+0

As [this comment](http://stackoverflow.com/questions/9049143/android-share-intent-for-a-bitmap-is-it-possible-not-to-save-it-prior-sharing/9049212 ?noredirect = 1#comment48438032_9049212)对你的回答说:“在应用的内部存储上创建世界可读的文件不是一个好主意。”使用[FileProvider](http://stackoverflow.com/a/30172247/3681880)更安全地完成它。 – Suragch

+0

嗨,汤姆,你的回答很棒!你知道这个工作所需的最低API版本是什么,即它能在Android 3.0+下工作吗?谢谢。 – middlehut

16

与其他应用程序共享文件的推荐方法是使用名为的FileProvider。这些文档相当不错,但有些部分有点棘手。这是一个总结。

搭建FileProvider在清单

<manifest> 
    ... 
    <application> 
     ... 
     <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.example.myapp.fileprovider" 
      android:grantUriPermissions="true" 
      android:exported="false"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/filepaths" /> 
     </provider> 
     ... 
    </application> 
</manifest> 

与您的应用程序包名称替换com.example.myapp

创建RES/XML/filepaths.xml

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <cache-path name="shared_images" path="images/"/> 
</paths> 

这告诉FileProvider哪里得到的文件共享(使用在这种情况下,缓存目录)。

保存图像到内部存储

// save bitmap to cache directory 
try { 

    File cachePath = new File(context.getCacheDir(), "images"); 
    cachePath.mkdirs(); // don't forget to make the directory 
    FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    stream.close(); 

} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

分享图像

File imagePath = new File(context.getCacheDir(), "images"); 
File newFile = new File(imagePath, "image.png"); 
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile); 

if (contentUri != null) { 

    Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file 
    shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri)); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri); 
    startActivity(Intent.createChooser(shareIntent, "Choose an app")); 

} 

进一步阅读