Null Pointer Exception while putting Image In Bitmap - android

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

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.

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

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

Is it possible to get the image path of an image in an image view?

I have set an image in an image view. Now I want to save this state and need to know the image path of that image. Is there a way to do that?
UPDATE:
// Decode, scale and set the image.
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
myBitmap.recycle();
myBitmap = null;
mImageView.setImageBitmap(scaledBitmap);
Not directly, once an image is set on an ImageView it is turned into a Drawable and all else is forgotten. However, you could use tags for this purpose. Any view can have a tag associated with it that represents some metadata about that view. You could do something like:
ImageView iv; //Declared elsewhere
Uri imagePath = Uri.parse("...");
//Set the image itself
iv.setImageURI(imagePath);
//Add the path value as a tag
iv.setTag(imagePath);
//Sometime in the future, retrieve it
Uri path = (Uri)iv.getTag();
You might also consider creating a subclass of ImageView to encapsulate this extra function to help make your code a little easier to read and maintain.
HTH

Set Background - Picture in Framelayout

Perhaps very simple, but not for me ;)
How do i change the Picture of a Framelayout in my sourcecode?
My first intention was:
mBackgroundDrawable = getResources().getDrawable(R.drawable.picture);
main.setBackgroundDrawable(mBackgroundDrawable);
but there i only can add pictures from my "RES/DRAWABLE-HDPI" folder.
I want to use my SD-Card as the destinated source.
Whats the right way?
Try this:
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
BitmapDrawable dr=new BitmapDrawable(bMap);
You'll need to convert your Bitmap to a BitmapDrawable before able to set it as a background.
val bitmap = BitmapFactory.decodeFile(this.file.absolutePath)
frameLayout.background = BitmapDrawable(context.resources, bitmap)

Categories

Resources