【问题标题】:write an image to external storage in android将图像写入android中的外部存储
【发布时间】:2013-12-09 23:25:59
【问题描述】:

我使用以下代码将图像写入 android 的外部存储:

File root = android.os.Environment.getExternalStorageDirectory(); 
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs(); 
fileName = "image_2.jpeg";
File file = new File(dir, fileName);

try {
    FileOutputStream outStream = new FileOutputStream(file);
    Bitmap bitmap = BitmapFactory.decodeFile("android.resource://com.mypackage.com/drawable/image_1");
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outStream);
    outStream.flush();
    outStream.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}   

此代码用于从drawable文件夹中读取image_1.jpg,然后将其写入外部存储中的下载文件夹,名称为image_2.jpeg。 (在外部存储中创建下载文件夹,并在该文件夹中创建一个名为 image_2.jpeg 的文件)。 此代码将产生一个((强制关闭))。下载文件夹已创建,image_2.jpeg 也已创建,但 image_2.jpeg 已损坏。

【问题讨论】:

  • "此代码将产生一个 ((force close))" -- 使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪。此外,请不要将用户的外部存储与随机目录混淆。在Context 上使用Environment.getExternalStoragePublicDirectory()getExternalFilesDir(),为您的文件提供更合理的位置。

标签: android image storage external


【解决方案1】:

可以通过BitmapFactory访问drawable文件夹中的这些图像,您可以将位图保存为PNG或JPG。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
    FileOutputStream out;
    out = new FileOutputStream(dest);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

别忘了添加 android.permission.WRITE_EXTERNAL_STORAGE 权限。

对于其他类型的图片,我认为将它们放入 assets 文件夹是更好的方法。

There is a sample here.

【讨论】:

    【解决方案2】:

    我对这段代码做了同样的事情。试试这段代码:

    String[] sampleImagesName = { "image2" };
    int[] sampleImages = { R.drawable.image1};
    File file;
    if (Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED)) {
        file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/download");
        if (!file.exists()) {
            file.mkdirs();
            SaveSampleToSD();
        }
    }
    private void SaveSampleToSD() {
            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/download";
    
            for (int i = 0; i < sampleImages.length; i++) {
                try {
                    File f = new File(path + "/", sampleImagesName[i] + ".jpg");
                    Bitmap bm = BitmapFactory.decodeResource(getResources(),
                            sampleImages[i]);
                    FileOutputStream out = new FileOutputStream(f);
                    bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
    
                    out.flush();
                    out.close();
    
                    Log.e("ImageSaved---------", "saved");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 2015-10-21
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多