【发布时间】:2013-10-18 12:39:52
【问题描述】:
我使用 httpurlconnection 下载了一个图像文件,然后将其保存在外部存储文件夹中以备后用
private Bitmap getBitmap(String path) {
File f = new File(path);
File tempFile = null;
Bitmap bitmap = null;
if (f.exists()) {
// from SD projects
if (f.isDirectory()) {
return null;
}
bitmap = decodeFile(f, 0);
if (bitmap != null)
return bitmap;
} else {
// from web
try {
URL imageUrl = new URL(path.toString());
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(TIMEOUT);
conn.setReadTimeout(TIMEOUT);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
tempFile = fileCache.getTempFile();
OutputStream os = new FileOutputStream(tempFile);
copyStream(is, os);
os.flush();
os.close();
is.close();
// LogUtils.d(TAG, "required size: " + requiredSize);
bitmap = decodeFile(tempFile, 0);
return bitmap;
} catch (Throwable ex) {
Log.e(TAG, "error decoding bitmap", ex);
if (ex instanceof OutOfMemoryError) {
thumbsCache.clear();
}
} finally {
}
}
return bitmap;
}
然后使用此代码获取位图文件缩略图:
public static Bitmap getThumbnail(ContentResolver cr, String path, int kind) {
Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] { path }, null);
try {
if (ca != null && ca.moveToFirst()) {
int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
ca.close();
return MediaStore.Images.Thumbnails.getThumbnail(cr, id, kind, null);
}
} catch (Exception e) {
Log.w(TAG, "exception getting thumbnail: "+path+ " kind : "+kind);
}
finally {
ca.close();
}
return null;
}
文件存在,我检查了 查询不为空 但查询是空的 - 没有行。 这是为什么呢?
如何获取下载图片的缩略图?
【问题讨论】:
标签: android bitmap thumbnails