我有一个从URL获取位图并将其存储在SD卡中的功能。但是我无法撤退他们。我将不胜感激任何帮助。

我的代码如下:

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;
}




这是decodeFile函数。它是从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;
}




而且我有一个处理目录选择等的文件缓存类。它如下:



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();
    }
}
}

最佳答案

public Bitmap createBitMap(){
    File file = new File("your sdcard path");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    return bitmap;
}

关于android - 如何从SD卡获取位图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22855983/

10-09 03:44