【问题标题】:Store Bitmap image to SD Card in Android在 Android 中将位图图像存储到 SD 卡
【发布时间】:2011-12-12 06:50:12
【问题描述】:

我的 android 代码遇到了一些奇怪的问题, 我在 Bitmap 变量中有一个图像,并希望将该文件保存到 SD 卡。 我的代码如下,

Bitmap IMAGE // Loaded from internet servers.;
try {
    File _sdCard = Environment.getExternalStorageDirectory();
    File _picDir  = new File(_sdCard, "MyDirectory");
    _picDir.mkdirs();

    File _picFile = new File(_picDir,  "MyImage.jpg");
    FileOutputStream _fos = new FileOutputStream(_picFile);
    IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
    _fos.flush();
    _fos.close();
    Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
    ex.printStackTrace();
    Toast.makeText(this, ex.getMessage(), 7000).show();
}

我使用 Sony Experia Arc 作为我的测试设备,当手机连接到我的电脑时,代码运行良好,它可以存储图像并显示在图库中。但是当我断开手机与电脑的连接并测试应用程序时,它并没有保存图片,也没有显示任何异常。

【问题讨论】:

  • 您是否在清单文件中添加了使用权限来写入外部存储。
  • 这可能会产生异常,但很好。 OP,logcat 输出中是否有任何有用的消息?
  • 顺便说一句,次要风格 nit:变量名中以 '_' 开头的那些不会增加任何可读性。我知道它们有时会在其他语言中使用,但我不会在 Java 中使用它们。

标签: android bitmap sd-card


【解决方案1】:

使用这个功能

 void saveImage() {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

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

检查此答案将提供更多详细信息Android saving file to external storage

【讨论】:

  • 还是一样的问题,
  • 当我从 Eclipse 运行应用程序时,它运行良好,但是当我从计算机断开电线并从手机加载应用程序时,它无法正常工作。
  • @Bhavin Mistry 是否在日志跟踪中显示任何错误?如果是,请发布。
  • 另外,String state = Environment.getExternalStorageState(); 是什么意思?等于? Environment.MEDIA_MOUNTED.equals(state)的值是多少
  • String state = Environment.getExternalStorageState() - 始终将状态显示为已安装。 Environment.MEDIA_MOUNTED.equals(state) 的条件始终保持为真。
【解决方案2】:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        //4
        File file = new File(Environment.getExternalStorageDirectory()+File.separator +        "image.jpg");
        try {
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            //5
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

【讨论】:

    【解决方案3】:
    **This Code Cover the Following Topics**
    
    1. Save a bitmap Image on sdcard a jpeg
    2. Create a folder on sdcard
    3. Create every file Separate name
    4. Every file save with date and time
    5. Resize the image in very small size
    6. Best thing image Quality fine not effected from Resizing
    
    
    The following method is used to create an image file using the bitmap
    

    public void createImageFromBitmap(Bitmap bmp) {

        FileOutputStream fileOutputStream = null;
        try {
    
            // create a File object for the parent directory
            File wallpaperDirectory = new File("/sdcard/Capture/");
            // have the object build the directory structure, if needed.
            wallpaperDirectory.mkdirs();
    
            //Capture is folder name and file name with date and time
            fileOutputStream = new FileOutputStream(String.format(
                    "/sdcard/Capture/%d.jpg",
                    System.currentTimeMillis()));
    
            // Here we Resize the Image ...
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100,
                    byteArrayOutputStream); // bm is the bitmap object
            byte[] bsResized = byteArrayOutputStream.toByteArray();
    
    
            fileOutputStream.write(bsResized);
            fileOutputStream.close();
    
    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
    
    
       and add this in manifest
    
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      相关资源
      最近更新 更多