首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android将位图保存到SD卡

Android将位图保存到SD卡
EN

Stack Overflow用户
提问于 2012-08-07 20:34:26
回答 2查看 47.3K关注 0票数 21

我有一个按钮,我想当我点击它时,图像会保存到SD卡(或内部存储,因为在htc one x中,我们没有像SD卡那样的外部存储)

这是我的代码:

代码语言:javascript
复制
            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();
                }
            }
        });

以及如何使消息出现在其中,上面写着“您的图像已保存”。就像一个警报,但持续了2秒钟,然后就消失了

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-07 20:37:07

尝尝这个

代码语言:javascript
复制
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();
   }
}

并将此添加到清单中

代码语言:javascript
复制
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

请看下面的答案Android saving file to external storage

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

代码语言:javascript
复制
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));
票数 92
EN

Stack Overflow用户

发布于 2012-08-07 20:37:03

使用Toast消息

喜欢

代码语言:javascript
复制
Toast.makeText(Your_class_name.this,
                    "Your image is saved to this folder", Toast.LENGTH_LONG)
                    .show();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11846108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档