如何将 imageView 图像保存到图库(Android 开发)

How do I save my imageView image into Gallery (Android Development)(如何将 imageView 图像保存到图库(Android 开发))
本文介绍了如何将 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 开发)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)