简体   繁体   中英

Why can't I call a method with a parameter of the wrong type?

I tried using:

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f)
{
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            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;
}

then

R.drawable.image = decodeFile(R.drawable.image);

How to use this method? R.drawable.image is integer, so it gives me error, how can I decode file? I add 240kb image to textview.

Well you've said yourself, R.drawable.image is an integer - but you're trying to pass it to a method accepting a File , and then assign the return value (a Bitmap ) back to it.

Additionally, catching FileNotFoundException like that and just swallowing it is a really bad idea - why don't you just declare that the method might throw the exception?

It's not clear what you're really trying to do - or what R.drawable.image is really meant to achieve - but in order to use the method you've got, you clearly need a File .

Are you trying to convert a drawable to a File and then that File to a Bitmap?

This will allow you to decode a drawable right to a Bitmap:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image );

I think you may be looking for something like this:

Resources res = getResources();
Drawable image = res.getDrawable(R.drawable.image);

Android allows you to access the images in your drawable folders using the Resources class. They also handle the scaling for you by using different folders for different resolution images. Additionally, if you have a widget defined in xml, you can assign it a source drawable that way using the android:src attribute, in your case it would be android:src="@drawable/image" . Take a look at

http://developer.android.com/guide/topics/resources/drawable-resource.html

and

http://developer.android.com/training/multiscreen/screensizes.html

for more information about how you can handle images with Android and how the OS helps you choose a properly scaled image, respectively.

Edit: One more thing, if you're just trying to set the image resource for something like an ImageView, you can use imageView.setImageResource(R.drawable.image) so that you don't have to create a Drawable or Resources object in your code. If you need to do something to the image before you set it though, there are corresponding setImageDrawable and setImageBitmap methods.

VM wont let us allocate 6291456 bytes

Your image is about 6 Megabyte uncompressed, see http://developer.android.com/training/displaying-bitmaps/index.html

And if you want to load a downscaled version of that image which might be required since you seem to be low on memory it would work with about the following code

/** decodes image from resources to max maxSize x maxSize pixels */
private Bitmap decodeFile(Context context, int resId, int maxSize) {
    Resources res = context.getResources();
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, o);

    // Find the correct scale value. It should be the power of 2.
    int scale = 1;
    while (o.outWidth / scale / 2 >= maxSize && o.outHeight / scale / 2 >= maxSize)
        scale *= 2;

    // Decode with inSampleSize
    o.inJustDecodeBounds = false;
    o.inSampleSize = scale;
    return BitmapFactory.decodeResource(res, resId, o);
}

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