0

I want to set a BitMap from resources but the name of the image came in a string. How can i set it. My code now is this.

                 String Nombre= results.getString(0);
                 RelativeLayout oneLayout = (RelativeLayout)findViewById(R.id.relative1);
                 ImageView imageView = new ImageView(getApplicationContext());
                 Bitmap bMap = BitmapFactory.decodeFile("res/drawable/"+Nombre);
                 Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre);
                 imageView.setImageBitmap(bMap);
                 oneLayout.addView(imageView);

This line is not working i know:

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre);

I need a form to do it, any help is wellcome

2 Answers 2

1

If I understand your question, you have the name of the image and want to get the resid and load the image?

Assuming you have an image with the file name in Drawable:

myimage.jpg

Try this snippet:

String nameOfImage = "myimage";
int resId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), resId);
Sign up to request clarification or add additional context in comments.

4 Comments

It says : No package identifier when getting value for resource number 0x00000000
Are you still doing: Bitmap bMap = BitmapFactory.decodeFile("res/drawable/"+Nombre);? Skip that and use the Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), resId); code. Also check if you really have your image in the drawable folder. If you know it's there, try cleaning the project and try again. Check out [stackoverflow.com/questions/5254100/… for an alternative.
It says the same with the other solution... look this is the code, and the image is there in the folder :/ String Nombre= results.getString(0); Resources res = getResources(); int resID = res.getIdentifier(Nombre , "drawable", getPackageName()); Drawable drawable = res.getDrawable(resID ); how can i clean the proyect? Thanks for the help pal.
I found the error, i was putting the extension on the image, like image.jpg... now is working, Thanks friend.
0

the problem as to why "Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre)" to not working is because the name "Nombre" has a capital letter in it,thats not allowed.Remove it,clean your project and then run it

1 Comment

I know, thats not the problem. Sorry is my bad english, but if you can, read the other response and you will understand, thanks fot your time :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.