将图像保存在Android画廊中 [英] save image in gallery android
本文介绍了将图像保存在Android画廊中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码保存图像:
I am using this code to save the image:
URL url = null;
try {
url = new URL("image");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream output;
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/folder name/");
dir.mkdirs();
File file = new File(dir, image + ".png");
Toast.makeText(HomeActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
try {
output = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (Exception e) {
e.printStackTrace();
}
问题是当此代码在棒棒糖设备上运行时,图像未显示在图库中.我必须安装文件管理器才能检查这些图像.
The issue is when this code runs on lollipop devices, Images are not showing in gallery. I have to install File Manager to check these images.
使用以下代码:
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "image";
图像保存在相机文件夹中.
Images are saved in camera folder.
我想在所有android设备中的画廊中显示具有特定文件夹名称的图像.
I want to show images in gallery with a specific folder name in all android devices.
请帮助.
推荐答案
public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
//Create Path to save Image
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
path.mkdirs();
File imageFile = new File(path, imgName+".png"); // Imagename.png
FileOutputStream out = new FileOutputStream(imageFile);
try{
bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
out.flush();
out.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch(Exception e) {
throw new IOException();
}
}
为我工作. 谢谢您的时间
Worked for me. Thanks for your time
这篇关于将图像保存在Android画廊中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文