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

CMA*_*CMA 1 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();
Run Code Online (Sandbox Code Playgroud)

默认存储是库中的"Camera"文件夹.

我的问题是:

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

Raj*_*ddy 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();
}
Run Code Online (Sandbox Code Playgroud)

并在清单中添加此项

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Run Code Online (Sandbox Code Playgroud)

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