简体   繁体   中英

get FilePath from drawable folder Android

I want to re size bitmap image... so for that I am using below code

   BitmapFactory.Options bmOptions = new BitmapFactory.Options();
   bmOptions.inJustDecodeBounds = true;
   int photoW = bmOptions.outWidth;
   int photoH = bmOptions.outHeight;
   int scaleFactor = Math.min(photoW / 100, photoH / 100);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);

but my problem is that i am getting image from drawable folder like this

 Bitmap icon = BitmapFactory.decodeResource(getResources(),
                    Const.template[arg2]);

so how can i convert this things into file path so i can set in the following line

        Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);  

and can get resizable image

I don't get it, why do you want to use decodeFile() ? I'm pretty sure you can use

BitmapFactory.decodeResource(getResources(), Const.template[arg2]), bmOptions)

Docs

The answer is become like below

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            BitmapFactory.Options bmOptions = new BitmapFactory.Options();

            // If set to true, the decoder will return null (no bitmap), but
            // the out... fields will still be set, allowing the caller to
            // query the bitmap without having to allocate the memory for
            // its pixels.
            bmOptions.inJustDecodeBounds = true;
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            // Determine how much to scale down the image
            int scaleFactor = Math.min(photoW / 100, photoH / 100);

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Const.template[arg2],bmOptions);

            Drawable draw = new BitmapDrawable(getResources(), bitmap);

            /* place image to textview */
            TextView txtView = (TextView) findViewById(R.id.imgChooseImage);
            txtView.setCompoundDrawablesWithIntrinsicBounds(draw, null,
                    null, null);
            position = arg2;
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM