如何将 imageView 图像保存到图库(Android 开发) [英] How do I save my imageView image into Gallery (Android Development)

查看:29
本文介绍了如何将 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 开发)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆