Ilario
Ilario

Reputation: 6079

Drawables with a space in the filename?

i'm developer an app that have more image (about 3000) that has space in filename like:

Im An image.jpg

being new to Android, I only found out now that are not accepted by the resources that have blank spaces or capital letters in the name, and this is a very big problem!

I use the same images in an application for iOS (without problems), and it would be almost impossible now to change all the names.

there is a way to solve this problem without renaming images?

if not, is there a way to quickly rename 3000 images?

thanks at all

Upvotes: 3

Views: 1251

Answers (2)

Ayoub
Ayoub

Reputation: 341

You could put them in the assets folder and use them fom there, but you will have to get each image as a bitmap without the possibility of using the R.drawable.image_name notation.

I use this method:

public static Bitmap getFoodImage(Context context, String foodType){
    InputStream bitmap = null;
    Bitmap bit = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    try {
        bitmap = context.getAssets().open(foodType.toUpperCase() + ".png");
        if(bitmap != null){

            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            int sWidth = metrics.widthPixels;

            options.inJustDecodeBounds = true;
            Bitmap dBit = BitmapFactory.decodeStream(bitmap, null, options);
            int intrinsicHeight = options.outHeight;
            int intrinsicWidth = options.outWidth;
            //int height = (int) (intrinsicWidth * proportion);
            int nHeight = (int) (intrinsicHeight/1.3);

            try {
                bitmap.reset();
            } catch (IOException e) {
                Log.e("SGNAM", e.getMessage());
            }

            options.inJustDecodeBounds = false;

            dBit = BitmapFactory.decodeStream(bitmap, null, options);

            Bitmap bP = Bitmap.createBitmap(dBit, 0, 0, intrinsicWidth, nHeight);

            if(sWidth < intrinsicWidth){
                bit = Bitmap.createScaledBitmap(bP, intrinsicWidth, intrinsicHeight, false);
            }
            else{
                bit = Bitmap.createScaledBitmap(bP, sWidth, intrinsicHeight, false);
            }
            bitmap.close();
            dBit = null;
            bP = null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bit;
}

As you can see i have all the image names in uppercase but you can skip that part if not needed (same for the png).

If you are in your onCreate method and the image you want is called An image.png (remember that in this example the .png is added by the method):

Context context = this;
Bitmap myBitmap = getFoodImage(context, An image);

You can set this bitmap as a resource for an imageView this way:

ImageView myImageView = findViewById(R.id.imageView);
myImageView.setImageBitmap(myBitmap);

This should do he trick.

Upvotes: 0

OrhanC1
OrhanC1

Reputation: 1410

there is a way to solve this problem without renaming images?

Nope. Res files can not have spaces.

Upvotes: 5

Related Questions