我正在创建一个应用程序,它从网上下载图像并将它们存储在SD卡中,然后在网格视图中显示它们。谷歌搜索了很长一段时间后,我发现可以使用内置的图库应用内容提供商,在存储图像的同时生成缩略图
MediaStore.Images.Media.insertImage(context.getContentResolver(),
filename.getAbsolutePath(), filename.getName(),
filename.getName());
我尝试使用它,但我发现图像在" DCIM /"文件夹(对我来说)并在" DCIM / .thumbnails /"中创建缩略图。除此之外,当我打开图库应用程序时,我会看到我下载的图像!
我的问题是:
- 我是否必须使用gallery app内容提供商,如果是,我如何自定义缩略图和图像的文件夹来源
- 否则,我如何生成缩略图(迷你类型,微观类似画廊应用程序)
答案 0 :(得分:1)
public Bitmap downloadImage() {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(this.getImageUrl());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Log.i("im connected", "Download");
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmImg;
}
public void saveImage(Context context, Bitmap bmImg) {
File filename;
String imagePath = IMAGE_DIR;
try {
createDir(IMAGE_DIR);
filename = new File(imagePath, this.getImage());
FileOutputStream out = new FileOutputStream(filename);
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
generateThumb(bmImg);
} catch (Exception e) {
e.printStackTrace();
}
}
public Bitmap generateThumb(Bitmap bitmap) {
File filethumb;
Bitmap thumb = null;
String thumbPath = THUMB_DIR;
try {
createDir(THUMB_DIR);
filethumb = new File(thumbPath, this.getImage());
FileOutputStream out2 = new FileOutputStream(filethumb);
thumb = Bitmap.createScaledBitmap(bitmap, 250, 340, false);
thumb.compress(Bitmap.CompressFormat.JPEG, 90, out2);
out2.flush();
out2.close();
} catch (Exception e) {
e.printStackTrace();
}
return thumb;
}
答案 1 :(得分:-1)
Bitmap thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filename.getAbsolutePath()), 80, 80);