How to load an ImageView from a png file? - android

I take a picture with the camera using
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, 22 );
When the activity completes, I write the bitmap picture out to a PNG file.
java.io.FileOutputStream out = openFileOutput("myfile.png", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
That goes OK, and I can see the file is created in my app private data space.
I'm having difficulty when I later want to display that image using an ImageView.
Can anyone suggest code to do this?
If I try to create a File with path separators in, it fails.
If I try to create a Uri from a name without separators, that fails.
I can open the file OK using:
java.io.FileInputStream in = openFileInput("myfile.png");
But that doesn't give me the Uri I need to set an image with
iv.setImageURI(u)
Summary: I have the picture in a png file in private app data. What's the code to set that into an ImageView?
Thanks.

Try BitmapFactory.decodeFile() and then setImageBitmap() on the ImageView.

Also possible:
java.io.FileInputStream in = openFileInput("myfile.png");
iv.setImageBitmap(BitmapFactory.decodeStream(in));

iv.setImageURI(Uri.fromFile(in));

Why not this way:
ImageView MyImageView = (ImageView)findViewById(R.id.imageView1);
Drawable d = Drawable.createFromPath( PATH TO FILE );
MyImageView.setImageDrawable(d);

bitmap = BitmapFactory.decodeFile(imageInSD);

There should be no difference between decodeStream() and decodeFile(). decodeFile() method opens an inputstream and calls decodeStream(). This was already answered at this link

Related

How do I send bitmap data using intent to another screen and load it into a Image View in kotlin using .putExtra()?

I have searched online for a tutorial on how to send a bitmap data over to another activity using the putExtra() method in kotlin, but nothing seems to pop up. I do not know how to code in java so all the tutorials and stack overflow threads that talk about converting to a byte array do not help much. Can someone please help me with maybe a solution?
What I did so far (My project is in no regards to this post) is I saved a image using my camera as a bitmap using the code:
val thumbnail : Bitmap = data!!.extras!!.get("data") as Bitmap
(the key word "data" is a intent inside the upper override function (onActivityResult))
This saves a bitmap of the image I took with the camera and now I tried to send it over, using the putExtra() command as so:
var screenSwitch2 = Intent(this#MainActivity,mlscreen::class.java)
screenSwitch2.putExtra("bitmap", thumbnail)
On the other screen "mlscreen" I tried to recover the data using a intent.getStringExtra("bitmap")
val thumbnail = intent.getStringExtra("bitmap")
and then I set my image view in the "mlscreen" using the setImageBitmap method as here:
iv_image.setImageBitmap(thumbnail)
I got the error that I was looking for a bitmap data and not a string data for the method. I knew this would occur though because I had to use intent.getSTRINGExtra which would mean its converting it to a string I presume.
Any help with this would be appreciated! Thanks.
There are 3 ways to do that :
1 - Using Intent (not recommended)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
intent.putExtra("image", b);
startActivity(intent);
2 - Using static (not recommended)
public static Bitmap thumbnail;
3 - Using Path (recommended)
Save your Bitmap as an image file in specific folder (make it invisible to the users).
Get the path from the saved file.
Use intent.putExtrat("imagePath",path);.
Use BitmapFactory.decodeFile(filePath); to get the Bitmap from the path.
Remove the path.

How to get thumbnail of video file in android

I have a video file in my sdcard. I would like to show a preview of this video in my ImageView . I know that there is a API:
ThumbnailUtils.createVideoThumbnail(videoFile,
MediaStore.Images.Thumbnails.MINI_KIND)
but it always returns null, is there any alternative?
Its working for me
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MINI_KIND);
please check once, if you are passing path of video file .
MediaStore.Images.Thumbnails.MINI_KIND and MediaStore.Video.Thumbnails.MINI_KIND are both integers with value 1
So try this first
Bitmap bmp = ThumbnailUtils.createVideoThumbnail(videoPath,1);

Creating Image instance from drawable images in android

i am creating digital signature app using iTextG jar, to add water mark on the signature field, iText's
appearance.setImage(Image.getInstance(signedImagePath));
Requires a path of the watermark image, i want to use image from drawable folder of android, kindly suggest me i am not getting how to create Image instance from drawable, suggest if any other option is there?
You use ImageInstance something like this.
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.yourimage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
Hope this helped.
http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options)

Store image in internal storage using "bitmap.compress()" and display in ImageView using "setImageUri()"?

I am opening the gallery and getting the path of an image when it is selected. Then I create a Bitmap from this path and store this bitmap in internal storage through bitmap.compress() function. Is it possible to display this image in ImageView by specifying the path of this stored bitmap in setImageUri() function of ImageView ?
Bitmap bmap = BitmapFactory.decodeFile(selectedImagePath);
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
bmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
Here, fileName is an integer (actually an ID for a widget) converted into String.
Would it be possible to load this image in ImageView like this:
File internalFile = getFileStreamPath(fileName);
Uri internal = Uri.fromFile(internalFile);
imageView.setImageUri(internal);
I know I can set the image using setImageBitmap() function but that would require me to read the Bitmap from the file and pass it through the Parcel object which leads to Failed Binder Transaction error when the images are large. I am making a widget which displays an image through ImageView.
The setImageUri() method is not intended to be used that way, see this question and answers. The best way seems to be scaling the bitmap to the view before calling setImageBitmap(), e.g. by using Bitmap.createBitmap() with a Matrix parameter

Is it possible to retrieve an image from the data folder in an android app?

I want to retrieve an image from my data/data/com.apps.myapp/images folder and display it in an ImageView. Any clue?
Try this :
Bitmap bitmap = BitmapFactory.decodeFile("data/data/com.apps.myapp/images/img.png");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bitmap);
There are several components involved in this.
To get the path of your data folder you can use the method getDir in the Context.
Now you have to know the file name and open an stream here again the Context class is your friend. Now the stream can be decoded into a Bitmap via a Bitmap Factory.
After you got a Bitmap create a BitmapDrawable from it and pass it to your ImageView

Categories

Resources