android:将图像保存到SD卡中的特定文件夹

时间:2012-01-23 08:55:36

标签: android

我这里有一个代码片段,用于在SD卡上保存位图:

String filename = String.valueOf(System.currentTimeMillis()) ;
ContentValues values = new ContentValues();   
values.put(Images.Media.TITLE, filename);
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
values.put(Images.Media.MIME_TYPE, "image/jpeg");

Uri uri = getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
    outStream.flush();
    outStream.close();

默认存储空间是图库中的“相机”文件夹。

我的问题是:

使用上面的代码,我可以将我的图像保存到我的SD卡上的其他文件夹中吗?

3 个答案:

答案 0 :(得分:6)

试试这个

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);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

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

并在清单

中添加此内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

在这里查看我的答案:https://stackoverflow.com/a/7887114/964741

答案 1 :(得分:1)

请尝试以下代码,

File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath());
        dir.mkdirs();

    File out = new File(dir,filename);
    try {
        out.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    DataOutputStream fo = null;

        try {
            fo = new DataOutputStream( new FileOutputStream(out));
            //write what you want to fo
            fo.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

答案 2 :(得分:0)

请尝试以下代码。

URL imageUrl = new URL("url");

Bitmap bitmap=BitmapFactory.decodeStream(imageURLFacebook.openConnection().getInputStream());

saveToInternalStorage(bitmap);//call method and pass bitmap

保存在内部存储空间的方法

 private String saveToInternalStorage(Bitmap bitmapImage) {
        File directory = Environment.getExternalStorageDirectory();
        File childDirectory = new File(directory, "FolderName");
        if (!childDirectory.exists()) {
            childDirectory.mkdirs();
        }

        // Create imageDir
        File mypath = new File(childDirectory, "profile.jpeg");

        try {
            FileOutputStream fos = new FileOutputStream(mypath);
            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return String.valueOf(mypath);
    }

在清单

中添加此内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />