How do I save my imageView image into Gallery (Android Development)(如何将 imageView 图像保存到图库(Android 开发))
问题描述
我正在尝试创建一个 onClick 事件,通过单击按钮将图像视图保存到手机图库中,下面是我的代码.它没有保存到图库中,谁能帮我找出原因?
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()
将返回一个 String != null
如果图像已经真正保存.另外:需要清单中的权限为android.permission.WRITE_EXTERNAL_STORAGE"请注意,这会将图像置于画廊中已有图像列表的底部.
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.
希望这会有所帮助.
这篇关于如何将 imageView 图像保存到图库(Android 开发)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 imageView 图像保存到图库(Android 开发)
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01