如何将 ImageView 保存为图像?
我有一个具有共享意图的 ImageView(效果很好,可以调出我可以共享图像的所有应用程序),但是,我无法共享照片,因为它在我的手机上没有路径。如何将 ImageView 保存到手机上?下面是我的代码。
public void taptoshare(View v)
{
View content = findViewById(R.id.myimage);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/DCIM/Camera/image.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
shareIntent.setData(phototUri);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
更新 好的,所以我想通了。现在我有一个新问题,如何将此图像保存到新文件夹?
I have an ImageView with a share intent( which works great, brings up all the apps I can share the image with), however, I can not share the photo because it has no path on my phone. How do I go about saving the ImageView on my phone? Below is my code.
public void taptoshare(View v)
{
View content = findViewById(R.id.myimage);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/DCIM/Camera/image.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
shareIntent.setData(phototUri);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
UPDATE
Ok, so I figured it out. Now I have a new question, how would I go about saving this image to a new folder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保存和加载时,首先需要获取系统的根路径。我就是这样做的。
When saving and loading, you need to get the root path of the system, first. This is how I'd do it.
我遇到了一些无法解决此问题的解决方案。
这是一个对我有用的解决方案。一个问题是您需要将图像存储在共享或非应用程序私有位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
许多建议都说存储在
Apps
“私有”缓存中位置,但这当然不是可通过其他外部应用程序访问,包括正在使用的通用共享文件意图。当您尝试此操作时,它将运行,但例如 dropbox 会告诉您该文件不再可用。/* 步骤 1 - 使用下面的文件保存函数在本地保存位图文件。 */
/* 步骤 2 - 将非私有绝对文件路径共享到共享文件意图 */
/* 保存图像功能 */
/* 步骤 3 - 处理共享文件意图结果。需要远程临时文件等。 */
/* UTILS */
我希望对某人有所帮助。
I've come across a couple solutions which are not solving this problem.
Here is a solution that worked for me. One gotcha is you need to store the images in a shared or non app private location (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
Many suggestions say to store in the
Apps
"private" cache location but this of course is not accessable via other external applications, including the generic Share File intent which is being utilised. When you try this, it will run but for example dropbox will tell you the file is no longer available./* STEP 1 - Save bitmap file locally using file save function below. */
/* STEP 2 - Share the non private Absolute file path to the share file intent */
/* SAVE IMAGE FUNCTION */
/* STEP 3 - Handle Share File Intent result. Need to remote temporary file etc. */
/* UTILS */
I hope that helps someone.