Android 将位图保存到 SD 卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11846108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:59:59  来源:igfitidea点击:

Android saving Bitmap to SD card

androidimagebitmapalert

提问by John Jared

I have a button, and I want when I click on it the image gets saved into the sd card ( or the internal storage, as in htc one x we don't have an external storage like an sd card )

我有一个按钮,我希望当我点击它时图像被保存到 SD 卡(或内部存储,如在 htc one x 我们没有像 SD 卡这样的外部存储)

this is my code:

这是我的代码:

            sd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                MpClick.start();
                File myDir=new File("/sdcard/Saved_images");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-"+ n +".jpg";
                File file = new File (myDir, fname);
                if (file.exists ()) file.delete (); 
                try {
                       FileOutputStream out = new FileOutputStream(file);
                       bitMapToShare.compress(Bitmap.CompressFormat.JPEG, 600, out);
                       out.flush();
                       out.close();

                } catch (Exception e) {
                       e.printStackTrace();
                }
            }
        });

and how do I make a message appears in it it's written "Your image was saved." like an alert but for 2seconds and then disappears

以及如何在其中显示一条消息,上面写着“您的图像已保存”。像警报一样,但持续 2 秒然后消失

回答by RajaReddy PolamReddy

try this

尝试这个

private void SaveImage(Bitmap finalBitmap) {

   String root = Environment.getExternalStorageDirectory().toString();
   File myDir = new File(root + "/saved_images");    
   myDir.mkdirs();
   Random generator = new Random();
   int n = 10000;
   n = generator.nextInt(n);
   String fname = "Image-"+ n +".jpg";
   File file = new File (myDir, fname);
   if (file.exists ()) file.delete (); 
   try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

   } catch (Exception e) {
       e.printStackTrace();
   }
}

and add this in manifest

并将其添加到清单中

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Look at this answer Android saving file to external storage

看看这个答案Android将文件保存到外部存储

EDIT: By using this line you can able to see saved images in the gallery view.

编辑:通过使用此行,您可以在图库视图中查看保存的图像。

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));

回答by Numair

Use Toast message

使用Toast 消息

like

喜欢

Toast.makeText(Your_class_name.this,
                    "Your image is saved to this folder", Toast.LENGTH_LONG)
                    .show();