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

北慕城南 提交于 2019-12-12 09:11:26

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/7281242/android-how-can-i-get-a-image-that-is-in-drawable-its-size-in-kb-name-and-ext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!