Android:将图像保存到SD卡中的特定文件夹 [英] android: save images to specific folder in the sd card
本文介绍了Android:将图像保存到SD卡中的特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这里有一个代码段,可将位图保存在sd卡上:
i have here a code snippet that saves a bitmap on sd card:
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();
默认存储为图库中的相机"文件夹.
the default storage is the "Camera" folder in the gallery.
我的问题是:
使用上面的代码,我可以将图像保存到SD卡上的其他文件夹中吗?
using the code above, can i save my images to a different folder on my sd card?
推荐答案
尝试一下
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
check my answer here : https://stackoverflow.com/a/7887114/964741
这篇关于Android:将图像保存到SD卡中的特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文