Programmatically change background / source for ImageView in android - 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);

Related

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.

Android Bitmap on Canvas from External File

I use the following code to get a bitmap from the resources folder put it into a drawable and paint it to the canvas
Drawable f = getResources().getDrawable(R.drawable.harris1);
f.setBounds(a,120,a+200,270);
f.draw(canvas);
I want to be able to get the bitmap from my pictures directory on my device and paint it on the canvas
I get the filepath as follows
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = "harris1.jpg";
File file = new File(path, name );
String fileName = String.format("%s/" + name, path);
in this instance filename is equal to "/storage/emulated/0/Pictures/harris1.jpg"
How do i change the line
Drawable f = getResources().getDrawable(R.drawable.harris);
to use the filname of the picture on the device?
Any Help Appreciated
Mark
How about something like:
Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
canvas.drawBitmap(bitmap, ...);
Use BitmapFactory there's an example you can follow
Reading an image file into bitmap from sdcard, why am I getting a NullPointerException?
You might like this bit of code:
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
BitmapDrawable imagePath = new BitmapDrawable(getResources(), "/storage/emulated/0/Pictures/harris1.jpg");
myImageView.setImageBitmap(imagePath.getBitmap());

Null Pointer Exception while putting Image In Bitmap

I am facing with the NullpointerExcetion while putting image in Bitmap. I am getting Image from SD card.
Uri path = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
imageFile = new File(getRealPathFromURI(path));
if(imageFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(myBitmap);
}
Wild guess: myImage is null. Check that you're importing the correct R.
If you are in an Activity, check if imageView1 is in the layout you passed to setContentView method.
If you are using a fragment or have inflated programmatically a layout, then use view.findViewBy(R.id.imageView1) instead

I am getting bitmap from SD card by path and set to ImageView

I am getting bitmap from SD card by path and set to ImageView.. but ImageView is not shown bitmap on activity:
MainActivity.mBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
ImageView imageView1 = (ImageView) findViewById(R.id.image1);
imageView1.setImageBitmap(MainActivity.mBitmap);
why you are not setting the Image view using path
imageView1.setImageURI(mediaFile.getAbsolutePath());

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

Categories

Resources