user924941
user924941

Reputation: 1011

Android : How can i get a image that is in drawable its size in KB, name and extension

I have a pic in res/drawable-hpdi/pic.jpg, then i have a alert box that should display the image height width size name and extension, i just cant figure out how to get it act like a file and get its size etc.

Bitmap bMap = BitmapFactory.decodeResource(getResources(),
            R.drawable.pic);

    File file=new File("res/drawable-hdpi/pic.jpg");
    long length = file.length();
    if (!file.exists()) {
        length = -1;
    }
    imgInfo = "Height: " + bMap.getWidth() + "\n" + "Width: " + bMap.getHeight()
            + "\n" +"Size: " + length;

i get the height and width i cant get the rest can someone help me ?

Upvotes: 2

Views: 728

Answers (1)

San Francesco
San Francesco

Reputation: 646

ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileOutputStream fo = null;

    bMap.compress(Bitmap.CompressFormat.JPEG, 40, baos);        


    File dst = new File (photoName + ".jpg");
    try 
    {
    dst.createNewFile();
    //write the bytes in file       
    fo = new FileOutputStream(dst);
    fo.write(baos.toByteArray());
    }
    catch (IOException e)
    {
        Log.e("Photo Convert","Error creating file. Check: "+dst, e);
    }

After that, you'll be able to check file dst size and other stuff. Hope it'll help

Upvotes: 1

Related Questions