如何保存我的ImageView图像到画廊(Android的发展) [英] How do I save my imageView image into Gallery (Android Development)
问题描述
我想创建一个onClick事件来保存的ImageView成一个点击按钮的手机图库,下面是我的code。它不保存到画廊,谁能帮助我弄清楚为什么?
sharebtn.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图B){
// TODO自动生成方法存根
//试图保存图片 B = findViewById(R.id.imageView);
b.setDrawingCacheEnabled(真);
位图的位图= b.getDrawingCache();
//文件文件=新的文件(/ DCIM /摄像机/ image.jpg文件);
文件根= Environment.getExternalStorageDirectory();
文件cachePath =新的文件(root.getAbsolutePath()+/DCIM/Camera/image.jpg);
尝试
{
cachePath.createNewFile();
FileOutputStream中的ostream =新的FileOutputStream(cachePath);
bitmap.com preSS(比较pressFormat.JPEG,100,ostream的);
ostream.close();
}
赶上(例外五)
{
e.printStackTrace();
} }
});
我这样做是为了保存形象的画廊。
私人无效saveImageToGallery(){
imageview.setDrawingCacheEnabled(真);
位图B = imageview.getDrawingCache();
Images.Media.insertImage(getActivity()getContentResolver(),B,标题,描述);
}
insertImage()
将返回字符串!= NULL
如果图像已被真的救了。
另外:需要在清单许可为android.permission.WRITE_EXTERNAL_STORAGE
并注意,这使该图像在已在库中的图像的列表的底部。
希望这有助于。
I am trying to create an onClick event to save an imageview into the phone Gallery by the click of a Button, below is my code. it does not save into the Gallery, can anyone help me figure out why?
sharebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View b) {
// TODO Auto-generated method stub
//attempt to save the image
b = findViewById(R.id.imageView);
b.setDrawingCacheEnabled(true);
Bitmap bitmap = b.getDrawingCache();
//File file = new File("/DCIM/Camera/image.jpg");
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try
{
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
I do this to save Image in gallery.
private void saveImageToGallery(){
imageview.setDrawingCacheEnabled(true);
Bitmap b = imageview.getDrawingCache();
Images.Media.insertImage(getActivity().getContentResolver(), b,title, description);
}
insertImage()
will return a String != null
if image has been really saved.
Also: Needs permission in the manifest as "android.permission.WRITE_EXTERNAL_STORAGE"
And note that this puts the image at the bottom of the list of images already in the gallery.
Hope this helps.
这篇关于如何保存我的ImageView图像到画廊(Android的发展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!