Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像写入android中的外部存储器_Android_Image_Storage_External - Fatal编程技术网

将图像写入android中的外部存储器

将图像写入android中的外部存储器,android,image,storage,external,Android,Image,Storage,External,我使用以下代码将图像写入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 { FileO

我使用以下代码将图像写入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的文件)。
此代码将生成((强制关闭))。下载文件夹已创建,图像\u 2.jpeg也已创建,但图像\u 2.jpeg已损坏。

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\u外部存储权限

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


我用这段代码做了同样的事情。请尝试以下代码:

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();
            }

        }
    }
String[]sampleImagesName={“image2”};
int[]sampleImages={R.drawable.image1};
文件;
如果(Environment.getExternalStorageState().equals(
环境.媒体(U){
file=新文件(Environment.getExternalStorageDirectory()
.getAbsolutePath()+“/下载”);
如果(!file.exists()){
mkdirs()文件;
SaveSampleToSD();
}
}
私有void SaveSampleToSD(){
String path=Environment.getExternalStorageDirectory()
.getAbsolutePath()+“/download”;
对于(int i=0;i
“此代码将生成((强制关闭))”——使用LogCat检查与崩溃相关的Java堆栈跟踪。另外,请不要用随机目录将用户的外部存储混乱起来。使用
Context
上的
Environment.getExternalStoragePublicDirectory()
,或
getExternalFilesDir()
,以获得文件的更合理位置。