dynamically load imageView - android

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

Related

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;

How to set bitmap size that fill parent?

I want to creat a bit map in the size that fill parent of the app window. how can i do that?
Bitmap maskImage = BitmapFactory.decodeResource(getResources(), R.drawable.img);
ImageView on = (ImageView) findViewById(R.id.on);
Bitmap result = Bitmap.createBitmap(maskImage.getWidth(), maskImage.getHeight(), Bitmap.Config.ARGB_8888);
on.setImageBitmap(result);
Only a single additional line is needed:
Once you have your ImageView:
ImageView on = (ImageView) findViewById(R.id.on);
on.setImageBitmap( . . . );
on.setScaleType(ScaleType.FIT_XY);
ScaleTypes are options for scaling the bounds of your image. You can read more about ScaleTypes in the official docs.

OOM Error when loading Drawable

My Image has the size: 2480px x 3508px. I know that this is a very big image for android and so I try to resize it.
I'm using the PhotoView library too by the way.
Here i start resize the Image:
Drawable resizedDrawable = resize(getResources().getDrawable(R.drawable.handskizze), 50, 50);
skizze.setImageDrawable(resizedDrawable);
mAttacher = new PhotoViewAttacher(skizze);
The method which resizes the drawable:
private Drawable resize (Drawable image, int dstWith, int dstHeight) {
Bitmap b = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b,dstWith,dstHeight,false);
return new BitmapDrawable(getResources(),bitmapResized);
}
The Error:
at de.example.app.TouchMoveImage.onCreate(TouchMoveImage.java:70)
So why i can't resize the Image? I know that is it to big for android. But it crashes before it starts resizing.

Programmatically change background / source for ImageView in android

I am creating an android app where I want to display an image full screen. The image is stored in the internal storage of an android phone and the image to be displayed can vary each time. In the layout XML file for that activity, I added an image view tag as shown below:
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/android" />
What shall I type for the android:src ? Any help & suggestions would be appreciated.
Optional: Remove the line
android:src="#drawable/android"
//This can be kept as a default image in case the file you want does not exist.
from the XML layout. Since the image may vary every time, you need to use
File file = new File("/sdcard/Images/test_image.jpg");
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView image = (ImageView) findViewById(R.id.imageview1);
image.setImageBitmap(bitmap);
}
try the below code to set Bitmap images from a file stored inside a SD-Card .
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
this works at runtime
You can do this at runtime, Try This:
String pathName = "/sdcard/abc.png";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
ImageView view = (ImageView) findViewById(R.id.container);
view.setBackgroundDrawable(bd);

Display image from resources with the name in a string

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

Categories

Resources