How do I display images from the phone's internal storage in an ImageView on android?
No image is loaded using the following code on my phone running on Marshmallow.
String path = Environment.getExternalStorageDirectory()+ "/MyFolder/final_photo";
File imgFile = new File(path);
if (imgFile.exists()) {
imageView.setImageBitmap(decodeFile(imgFile));
}
Your path doesn't contain a String to an Actual Image!
Images end with .jpg, .png etc.
As you also said that your Phone is on Marshmallow, you should also Allow the Storage permissions to your App!
Related
i am downloaded and saved images from web to my app cache folder. Now i am trying to convert particular cache folder image(PREM.jpg) into bitmap. For that i am using bellow code.
FileCache file = new FileCache(Context);
File f = file.getFile("PREM.jpg");
Bitmap bitmap = decodeFile(f);
System.out.println("Exist status. " + f.exists());
But i am always getting file not exist (Exist status. false).
Actually "PREM.jpg" file name showing some thing like "-1006685176" in the cache folder.
This is my cache filder
Can you please help me how to i convert that particular image into bitmap.
Thank you everyone.
Prem
I am creating an app that takes pictures then i read them,
when pictures are stored in SD card i can read them fine but when they are stored in phone memory i can find them (newFile displays correct path) but i don't see them in the image view. In samsung phones they are stored in /storage/emulated/0/Pictures/AppImages in cases like this is when i can't read and display in ImageView
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AppImages");
final ImageView newImage = new ImageView(this);
File newFile = new File(mediaStorageDir+File.separator+files.getString(2));
//I have tried this 3 options one at a time
final Bitmap bmp = BitmapFactory.decodeFile(newFile.getAbsolutePath());
final Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(newFile));
final Bitmap bmp = BitmapFactory.decodeFile(newFile.getPath());
newImage.setImageBitmap(bmp);
Have you tried MediaStore?
Look at this:
MediaStore.Images.Media.EXTERNAL_CONTENT_URI //from external storage
MediaStore.Images.Media.INTERNAL_CONTENT_URI //from internal storage
Take a look at this link
The problem was i was displaying the full pictures in the gallery, resizing them to thumbnails size fixed the issue.
Toast.makeText(this, Environment.getExternalStorageDirectory(),
Toast.LENGTH_LONG).show();
File file = new File(Environment.getExternalStorageDirectory() +
"/whatsupv2/abc.jpg");
Bitmap mybitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageURI(Uri.fromFile(file));
the above code is working for android 3.3 but not in 4.1.2 where we have two storage directories... i have checked abc.jpg is there in dir .. and path given is correct.. but imageview just show a white screen..
but not in 4.1.2 where we have two storage directoies
There is only one external storage directory in all Android devices, at least through Android 4.3.
First, make sure the image is in the official external storage location.
Second, use new File(Environment.getExternalStorageDirectory(), "/whatsupv2/abc.jpg") rather than string concatenation.
Third, either use decodeFile() or setImageURI(), not both.
Fourth, examine LogCat for any messages.
I am working on an application, where i take pictures from a camera and store it in a separate folder in sd card. The problem what i am facing is, once the pictures are stored in a folder on sd card the Image folder is not being displayed in android native gallery.
This should be something similar to whatsapp image folder. how is it possible?
use the function MediaStore.Images.Media.insertImage to create a thumbnail and show the image in the default android gallery.
String imageName = "imagepath";
File sdImageMainDirectory = new File(imageName);
MediaStore.Images.Media.insertImage(getContentResolver(),
sdImageMainDirectory.getAbsolutePath(),sdImageMainDirectory.getName(), "description");
You need to let the system know that new media has been added before it will show up. Call this after you save the image.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
How to store image and audio files in android internal storage?
And how to retrieve back to display image in imageview?
You can write a file to your internal storage. Check this link
https://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Retrieve a File
ImageView image = new ImageView();
Bitmap bMap = BitmapFactory.decodeFile("/folder/yourfilename.png");
image.setImageBitmap(bMap);