Display image from resources with the name in a string - android

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

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);

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

Related

GetBitmap from ImageView

I know this question was asked a lot before but every time I try to retrieve the Bitmap from my ImageView it return null object
I tried :
ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
And
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
And also with glide.
Am I missing something or is there another way to achieve this ?
Thanks for help
Try this, hope it helps
ImageView imageView = findViewById(R.id.image);
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
Bitmap b = bd.getBitmap();
I have a kotlin solution.
val bitmap = binding.myImageView.getDrawable().toBitmap()
I hope that it has been useful.

How do I save the viewport bitmap of an imageview? - Android

I only want to save the bitmap of the image being displayed/cropped in the ImageView.
When I call this, it returns the entire bitmap:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Use this to get the bitmap from ImageView
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

How to convert imageurl to drawable?

First of all i searched a lot and didn't find solution for my case, i need to convert the imageUrl to drawable, what i tried as far :
File imageFile = DiskCacheUtils.findInCache(c.profilePhoto, com.nostra13.universalimageloader.core.ImageLoader.getInstance().getDiscCache());
String file_target = "file://" + imageFile.getPath();
BitmapDrawable bitDraw = (BitmapDrawable) BitmapDrawable.createFromPath(file_target);
if (profilePhotos.size() == 4) break;
BitmapDrawable drawable = bitDraw; //getResources().getDrawable(c.profilePhoto);
drawable.setBounds(0, 0, width, height);
profilePhotos.add(drawable);
Also i tried to load the image into bitmap via Glide, but didn't work.
How i can achieve that ?
Thanks in advance.
first convert URL to Bitmap and pass bitmap into these function
Drawable d= new BitmapDrawable(context.getResources(), bitmap);
return d;

dynamically load imageView

I have a layout where one imageView want dynamically load an image from drawable files. I want this I have a certain size imageView whatever drawable image, so you need to assign a size to upload. I searched the android developers but I've only seen functions to set the maximum height and width, but ir doesn't works.
I put the code:
image = getIntent().getExtras().getString("image");
ImageView paper = (ImageView)findViewById(R.id.imageView1);
Resources res = getResources();
int id = getResources().getIdentifier(image, "drawable", getPackageName());
Drawable drawable = res.getDrawable(id);
paper.setImageDrawable(drawable);
Can you help please?
thank you very much
You can use
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
// bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
bitmap = Bitmap.createScaledBitmap(bitmap, 250, 250, true);
img.setImageBitmap(bitmap);

How to set a bitmap from resource

This seems simple, I am trying to set a bitmap image but from the resources, I have within the application in the drawable folder.
bm = BitmapFactory.decodeResource(null, R.id.image);
Is this correct?
Assuming you are calling this in an Activity class
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
The first parameter, Resources, is required. It is normally obtainable in any Context (and subclasses like Activity).
Try this
This is from sdcard
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
This is from resources
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
If the resource is showing and is a view, you can also capture it. Like a screenshot:
View rootView = ((View) findViewById(R.id.yourView)).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
This actually grabs the whole layout but you can alter as you wish.
If you have declare a bitmap object and you want to display it or store this bitmap object. but first you have to assign any image , and you may use the button click event, this code will only demonstrate that how to store the drawable image in bitmap Object.
Bitmap contact_pic = BitmapFactory.decodeResource(
v.getContext().getResources(),
R.drawable.android_logo
);
Now you can use this bitmap object, whether you want to store it, or to use it in google maps while drawing a pic on fixed latitude and longitude, or to use some where else
just replace this line
bm = BitmapFactory.decodeResource(null, R.id.image);
with
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YourImageName);
I mean to say just change null value with getResources() If you use this code in any button or Image view click event just append getApplicationContext() before getResources()..
Using this function you can get Image Bitmap. Just pass image url
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

Categories

Resources