I download an image file using httpurlconnection, and then save it for later use in external storage folder
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;
}
and then getting the bitmap file thumbnail with this code:
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;
}
The file exists, i checked the query isnt null but the query is empty - no rows. why is that?
how can i get thumbnails for downloaded images ?