public Bitmap createBitMap(){
File file = new File("your sdcard path");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
return bitmap;
}
How to get bitmap from sdcard
-
27-06-2023 - |
Question
I have a function that gets bitmap from URL and stores them in sd-card. But i am unable to retreive them. I would appreciate any help.
My code goes as follows:
private static Bitmap getDrawableFromUrl(final String Url,final String videoID) {
Drawable d = null;
Bitmap bitmap = null;
bitmap = memoryCache.get(Url);
File f = fileCache.getFile(videoID);
bitmap = decodeFile(f);
if(bitmap != null)
{
Log.v(DBUG, "Seems got the file::");
return bitmap;
}
if(bitmap == null){
try {
System.out.println(Url);
d = Drawable.createFromStream(((java.io.InputStream) new java.net.URL(Url).getContent()),"name");
BitmapDrawable b = ((BitmapDrawable) d);
bitmap = b.getBitmap();
memoryCache.put(Url, bitmap);
fileCache.put(videoID, bitmap);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
return bitmap;
}
And here is decodeFile function. It is a function to decode files from sdcard..
private static Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
And i have a file cache class that handles the directory selection and so on. It goes as follows::
public class FileCache {
private File cacheDir;
private final static String DEBUG = FileCache.class.getSimpleName();
public FileCache(Context context){
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"TempImages1");
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url){
Log.v(DEBUG, "tring to get file f:::");
String filename=String.valueOf(url.hashCode());
File f = new File(cacheDir, filename);
return f;
}
public void clear(){
File[] files=cacheDir.listFiles();
if(files==null)
return;
for(File f:files)
f.delete();
}
public void put(String url, Bitmap bitmap) {
// TODO Auto-generated method stub
Log.v(DEBUG, "It has been called::::");
File dest = new File(cacheDir, url);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow