Java 加载内存位图并共享它
我已使用以下工具将位图保存到内部存储器:Java 加载内存位图并共享它,java,android,bitmap,inputstream,outputstream,Java,Android,Bitmap,Inputstream,Outputstream,我已使用以下工具将位图保存到内部存储器: private String saveToInternalSorage(Bitmap bitmapImage){ ContextWrapper cw = new ContextWrapper(mContext); // path to /data/data/yourapp/app_data/imageDir File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(mContext);
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
现在我想与大家分享:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_STREAM, file);
sharingIntent.setType("image/jpeg");
getContext().startActivity(Intent.createChooser(sharingIntent,"Erfolg teilen!"));
如何获取内部存储器的位图并将其“生成”为“文件”,以便共享
我尝试加载以下内容:
但这不起作用,如果我想分享它,它不会显示任何图片(在Facebook上,它说它无法加载图片)。
保存方法正确吗
我已使用将位图保存到内部存储器
您的ContextWrapper
没有明显的价值
如何获取内部存储器的位图并将其“生成”为“文件”,以便共享
您已将其作为文件保存。这就是saveToInternalSorage()
所做的
要使其可共享。但是,您可能需要从getDir(“imageDir”,Context.MODE\u PRIVATE)
切换到getFilesDir()
,根目录,因为额外的流应该包含一个URI,其中包含与意图相关联的数据流
因此,在您的示例中,用Uri.fromFile(file)替换文件:
您可能还需要授予此意向的收件人对URI执行读取操作的权限:
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
将
mode\u private
更改为mode\u world\u readable
也改变了:
File directory = cw.getFilesDir();
致:
我遇到了这个问题。我正在使用
Context.getFilesDir()
保存位图(并避免请求写入权限)。您需要在清单中使用文件提供程序
这是我的文件提供商
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
更多信息请点击此处:
内部存储上的文件是私有的;收件人将无法阅读。我如何才能更改它不是私有的?是的,他可能需要在意图中添加标志\u GRANT\u read\u URI\u权限。谢谢,已更新。这样做了,但仍然不能很好地工作(请参阅更新的问题)@Lars3n95不确定您为什么需要一个文件?当您使用上面的代码调用startActivity时会发生什么情况?我现在尝试加载它:ContextWrapper cw=new ContextWrapper(mContext);File directory=cw.getFilesDir();File File=新文件(目录“profile.jpg”);我不得不说我真的不知道为什么会有这个contextWrapper。@Lars3n95:“我不得不说我真的不知道为什么会有这个contextWrapper”--我也不知道。顺便说一句,这里有一个示例应用程序演示了
文件提供程序的用法:这是不推荐使用的,同样的问题是它无法加载。理论上,这将使它成为可能,而所谓的贬损也有点荒谬。然而,一些第三方应用程序不会费心检查它们是否真的可以读取文件,而是简单地假设它们不能读取属于其他人的私有存储文件,并且只接受位于外部存储上的文本文件。这在某些版本的Gmail中很常见,例如当试图提供附件时。但是通过蓝牙发送也不可能。保存和加载方法正确吗?如果您想解决其他应用程序的惰性假设,请将其放在外部存储或作为内容提供商,而不是尝试传递文本文件。如何从外部存储保存和加载它?我试过:stringpath1=Images.Media.insertImage(getContext().getContentResolver(),cs,“profile.jpg”,null);Uri文件=Uri.parse(路径1);这工作得很好,但后来我遇到了一个问题,当智能手机没有sd卡时,应用程序关闭了。
File directory = cw.getFilesDir();
cw.getDir ("profile.jpg", Context.MODE_WORLD_READABLE);
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<paths>
<files-path
name="my_images"
path="Screenshots/" />
</paths>
val shareIntent = Intent(Intent.ACTION_SEND)
.apply {
type = "image/jpeg"
putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(applicationContext, BuildConfig.APPLICATION_ID, file))
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(Intent.createChooser(shareIntent, "Share Image"));