Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a file from drawable

Tags:

android

I have a large number of resources in my drawable folder.All are having big size more than 500KB. I have to load all these 25 images all at once in a srollView. As usual I ran out of memory. Is there any way to reduce the size of the image programmatically.

I got this function but it's parameter is a File and I don't know how to create a File from a drawable.


private Bitmap decodeFile(File f){
    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (FileNotFoundException e) {
    }
    return b;
}

I have to go thorugh this screen cyclically many times after a number of traversal the system is showing Low memory and it is removing the other views in the back from the stack , but I actually need it. Please help me.

like image 353
James Avatar asked May 05 '11 08:05

James


1 Answers

You can open an InputStream from your drawable resource using following code:

InputStream is = getResources().openRawResource(id);

here id is the identifier of your drawable resource. for eg: R.drawable.abc

Now using this input stream you can create a file. If you also need help on how create a file using this input stream then tell me.

Update: to write data in a file:

try
    {
    File f=new File("your file name");
    InputStream inputStream = getResources().openRawResource(id);
    OutputStream out=new FileOutputStream(f);
    byte buf[]=new byte[1024];
    int len;
    while((len=inputStream.read(buf))>0)
    out.write(buf,0,len);
    out.close();
    inputStream.close();
    }
    catch (IOException e){}
    }
like image 60
mudit Avatar answered Oct 21 '22 16:10

mudit