如何将ImageView保存为图像?
我有一个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"));
}
}
UPDATE 好了,所以我想它了。现在我有一个新的问题,我将如何去将这个图像保存到一个新的文件夹?
我遇到了一些解决方案,它们没有解决这个问题。
这是一个适合我的解决方案。有一个问题是你需要存储在共享或非应用私人的位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
许多建议说,在Apps
“私有”缓存位置存储图像,但这当然不是通过其他外部应用程序,包括accessable正在使用的通用共享文件意图。当你尝试这个,它会运行,但例如dropbox会告诉你该文件不再可用。
/*第1步 - 使用下面的文件保存功能在本地保存位图文件。 */
localAbsoluteFilePath = saveImageLocally(bitmapImage);
/*第2步 - 分享非私人文件绝对路径共享文件意图*/
if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(localAbsoluteFilePath);
File file = new File(phototUri.getPath());
Log.d("file path: " +file.getPath(), TAG);
if(file.exists()) {
// file create success
} else {
// file create fail
}
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION);
}
/*保存图像功能*/
private String saveImageLocally(Bitmap _bitmap) {
File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS);
File outputFile = null;
try {
outputFile = File.createTempFile("tmp", ".png", outputDir);
} catch (IOException e1) {
// handle exception
}
try {
FileOutputStream out = new FileOutputStream(outputFile);
_bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (Exception e) {
// handle exception
}
return outputFile.getAbsolutePath();
}
/*步骤3 - 处理共享文件意图结果。需要远程临时文件等。*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intents
if (requestCode== Navigator.REQUEST_SHARE_ACTION) {
// delete temp file
File file = new File (localAbsoluteFilePath);
file.delete();
Toaster toast = new Toaster(activity);
toast.popBurntToast("Successfully shared");
}
}
/* * UTILS/
public class Utils {
//...
public static File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file =
new File(Environment.getExternalStorageDirectory(), albumName);
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
return file;
}
//...
}
我希望可以帮助别人。
我在步骤2(什么是实用程序?)出错:File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS); –
@DinIslamMilon,对此感到遗憾,自从此工作以来,它已经有一段时间了。我已经添加了我使用的custom utils静态类。它获取用户图片目录 – wired00
应该单独提出一个新问题,因为您无法为单个问题授予多个答案。但无论如何,File有一个mkdirs()方法可用于确保指定的文件夹存在。 – AlbeyAmakiir
好的,谢谢@AlbeyAmakiir!留意未来的帖子! – dabious