Android: Displaying .jpg image from sdcard using ImageView - android

I am trying to display a .jpg from my sd card into an imageview in my layout. I dont get any errors, but nothing gets displayed. I would appreciate any help. Thanks
EDIT:
layout1 = (LinearLayout) findViewById(R.id.layout1Activity);
String imgFile = Environment.getExternalStorageDirectory() + "/Apple.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(myBitmap);
layout1.addView(myImage);

do NOT access the SD card directly, try accessing it trough Environment. Like this:
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);

Related

Displaying image from gallery in Oncreate method without opening gallery intent

I know how to open gallery intent and to display in the imageview.
But I am facing problem in selecting image automatically in oncreate method and display in the imageview.
I have an image in gallery folder with name "myfile.jpg"
Can anyone guide me how to do it without openting gallery intent.
Thank you in advance
Here is solution :
ImageView image = (ImageView)findViewById(R.id.myImage);
File root = Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath();
path = path + File.separator + "myfile.jpg"; //where you image file located
Bitmap myBitmap = BitmapFactory.decodeFile(path);
image.setImageBitmap(myBitmap);

Displaying from external storage?

I have an image saved in my Pictures folder, how to display it in a imageview?
like:
imageview.setimage("//Pictures//cat.jpg)
I know it's not a correct code, but I want to achieve something like this, hope someone can help, thanks!
you first generate a bitmap from file path and then put that bitmap to imageview
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
Edit: Also add this permission in your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can set image like this from the sd card get path and create file variable and decode file using BitmapFactory set imageview image
String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));
First you passing wrong file path.
for Correct File path do like this
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());
then create your URL like.
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";
Then use like this.
File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));
Got it working with combination of the 2 posted codes, thanks for everyone, here's the working code:
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
image1.setImageBitmap(bitmap);

how to display image file save on internal storage in android

i have saved drawable images in internal storage by compressing them in bitmap format. the problem is that i want to retrieve those image files and wants to display them in grid view i am able to list the total no of files but i am not able to display those files in image view. here is my code
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File[] imageList = directory.listFiles();
if(imageList == null){
imageList = new File[0];
}
Log.i("My","ImageList Size = "+imageList.length);
imageView.setImageResource(imageAdapter. (....?) );
set Image(From Sd card) to imageview get the path of the image which is in sdcard. and you can assign it by creating Bitmap
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "your_image_name.png";
Bitmap bmp = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bmp);
Another way to set imageview you can also use third party library also which may have resize and Scaling options too. like ImageLoader

SkImageDecoder:factory returned null

I am trying to get the image from sdcard to display it in an imageview in android. Its showing error as SkImageDecoder:factory returned null in android emulator2.2. Here is my code to retrieve image from SDcard to imageview in android.
File imgFile = new File("/sdcard/wm.png");
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(myBitmap);
}
Please resolve this issue.
Use Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory instead of hard coding the path of the sd card (i.e "/sdcard/wm.png").

Set Imageview to show image in sdcard?

I have a image stored in SD card of my phone. I want to show it in a image view. I know the location of the file. On the oncreate of the activity is there a simple way to say something like
ImageView img = (ImageView) findViewById(R.id.imageView1);
String path = Environment.getExternalStorageDirectory().toString() "Images/image.jpg";
img.setsrc = path ;
Please let me know if there is any way to do this. Thank you.
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageView img;
img.setImageBitmap(bmp);
Hope this helps.
I have this snippet that may help:
File imageFile = new File("/sdcard/example/image.jpg");
if(imageFile.exists()){
ImageView imageView= (ImageView) findViewById(R.id.imageviewTest);
imageView.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
}
:)

Categories

Resources