2

I have problem when I try to set an image to a ImageView browsed from assets folder. When I place the same image in the drawable folder and use imageView.setImageResource(R.id.image); it is larger and fits the screen better than the image browsed by the previous method. Is there a way to resize the image so that it fits the screen exactly the same way as usual.

Here is the layout file and the code in java.

        AssetManager manager = getActivity().getAssets();       
        InputStream input = null;
        try {
            input = manager.open("images/" + mQuestion.getQImage() + ".png");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap image = BitmapFactory.decodeStream(input);
        ImageView qImageView = (ImageView) questionView.findViewById(R.id.ImageView_qImage);
        qImageView.setImageBitmap(image);



  <ImageView
    android:id="@+id/ImageView_qImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/questionTopBottomMargin"
    android:background="@color/radioButtonStrokeColor"
    android:contentDescription="@string/hello_world"
    android:padding="1px"
    android:scaleType="fitXY" />
samvel1024
  • 1,123
  • 4
  • 15
  • 39

4 Answers4

4

When you place an image in drawable folder then the images are classified on density of screen while if you put images in assest folder then android need to calculate density.

If you want to use images from assest folder then this code will help you.

Options opts = new BitmapFactory.Options();
opts.inDensity = DisplayMetrics.DENSITY_HIGH;
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, srcName, opts);

see here

Image loaded from assets folder comes in different size than res/drawable

Android: Accessing images from assets/drawable folders

Community
  • 1
  • 1
Key_coder
  • 534
  • 3
  • 27
0

If you place images in the drawables folder they will be scaled by some functions it they load them from resources. This will not happen with images in assets folder which you load yourself.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

Have a look at android docs


  1. You should always use images from the drawable folder
  2. There are different drawable folders in android (drawable-hdpi,drawable-mdpi,drawable-ldpi)
  3. Android will decide which folder image should be drawn depending on the phone requirements and resolution
  4. Use 9-patch image for auto-scaling
  5. When you load from assets, the "source" density is unknown, so this autoscaling is not performed. Hence, there is difference.
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Why should I always use drawable folder? Will it affect the performance of my application? Currently I have almost 1000 images. And uploading these into drawable folder will create a mess for me. – samvel1024 Oct 20 '14 at 10:51
  • @ Abrahamyan Samvel ..... If you have large number of images like 1000 ...... Look at caching the images in SD-CARD .... This will reduce the size of your `apk` file ! – Devrath Oct 20 '14 at 13:10
  • But can I protect them from being deleted or edited by the user – samvel1024 Oct 20 '14 at 13:12
  • @ Abrahamyan Samvel ..... I Don't have complete info on that ..... I think user can't manually access those data ! ... Using `MODE-PRIVATE` you can make so that only your `app` can access those `data` ..... Or Using `MODE-APPEND` other apps can access the `data` ! ..... What i can tell is if there is `huge number` of images better `download` them in your `splash screen` and use them because its much `feasible` ! – Devrath Oct 20 '14 at 13:22
0

You just define the imageView width and height in the xml layout then you should try this code it works perfectly for me

public Bitmap getImageBitmap(String imgName){
    AssetManager mgr = getAssets();
    InputStream inputStream = null;

    try {
        inputStream = mgr.open(imgName + ".png");
        return BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = getImageBitmap(imageName);
imageView.setImageBitmap(bitmap);
Asad Mukhtar
  • 391
  • 6
  • 18