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);
Related
I need to set a imageView dynamically. The image is at drawable. This code is ok:
Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
img_pergunta.setImageBitmap(bm2);
The problem is that I need to start from string, but this code doesn´t work:
File imgFile = new File("/drawable/image1.png");
Bitmap bm2 = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img_pergunta.setImageBitmap(bm2);
How do I solve it?
You can get the id (which you use in decodeResource()) using context.getResources().getIdentifier() and a name like image4.
You probably have given a bad path to the file, try
File imgFile = new File("android.resource://COM.EXAMPLE.PACKAGE/drawable/image1.png");
but replace the package name with correct one - yours
You can create new file by giving path as mentioned below,
File file = new File("android.resource://"+R.class.getPackage().getName()+"/" +resourceId);
resourceId can be replaced by R.drawable.resourceName
I have this code:
Bitmap readBitmapImage() {
String imageInSD = "/sdcard/mac/"+strURL;
BitmapFactory.Options bOptions = new BitmapFactory.Options();
bOptions.inTempStorage = new byte[16*1024];
bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);
return bitmap;
}
The decodeFile function returns a null value, which is assigned to bitmap. Why doesn't decodeFile return the Bitmap image of the file saved on the SDCard?
I am working with AndroidStudio.
The problem is with your image path, i think it's not returning the absolute path. So try to use this for getting your imageInSD path like below
String imageInSD=new File(getFilesDir(), "test.png").getAbsolutePath();
Here test.png is your image name and make sure it is present in your filepath.
Don't forget to add these permissions in your manifest while dealing with the sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Here i have stored an image by converting as string using 'base64 format' on my server,and i could able to display it in the same way on image view .But now i want download it to my sd card .I can see that some people downloading using 'URL'. But in my case there is no URL.I just converted the image into string and stored and able to display it on imageview by reconverting it.
this is the way i reconverted the string into image
//getting string from server using json parsor
String image=json_data.getString("img");
txt.setText(u);
//converting string to image
byte[] imageAsBytes = Base64.decode(p.getBytes(), 0);
im = (ImageView)this.findViewById(R.id.imageView1);
im.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0,imageAsBytes.length));
Use Image Loader libraries instead of yours.
https://github.com/nostra13/Android-Universal-Image-Loader
https://github.com/bumptech/glide
http://square.github.io/picasso/
THIS SNIPPET SHOULD HELP !
// give any name to file xxx.jpg
File filePath = new File(Environment.getExternalStorageDirectory()+"/name.jpg");
FileOutputStream os = new FileOutputStream(filePath, true);
EDIT
Bitmap x = BitmapFactory.decodeByteArray(imageasbytes , 0 , inageasbytes.length());
x.compress(
Bitmap.CompressFormat.JPEG, 85, os);
os.flush();
os.close();
Also , add the following permission .
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Also if you want to know how to recover it..
String imgFile = Environment.getExternalStorageDirectory() + "/name.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
Imageview.setimagebitmap(mybitmap);
I am using the ImageView to display a image with a image stored in SD card. Like this-
ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position];
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
grp_icon.setImageBitmap(bitmap);
And I am trying to get the image location to display the same image in another activity by sending the location in Intent. But it gives a different location Why?
The code where I get the ImageView image location is
String groupName=((TextView) v.findViewById(R.id.grid_item_label)).getText().toString();
ImageView grp_image=(ImageView)v.findViewById(R.id.grid_item_image);
Bitmap b=((BitmapDrawable)grp_image.getDrawable()).getBitmap();
Uri image=getImageUri(getActivity().getApplicationContext(), b);
String path=image.toString();
Intent toMangeGroup=new Intent(getActivity(),ManageGroup.class);
Bundle bundle=new Bundle();
bundle.putString("grp_name", groupName);
bundle.putString("grp_image", path);
toMangeGroup.putExtras(bundle);
startActivity(toMangeGroup);
Here I am getting the path as "content://media/external/images/media/42424" but the actual path of the image is "/sdcard/Letsmeet/letsmeet_media/group_images/1395247992445"
System.out.println("this is path where stored.."
+ Environment.getExternalStorageDirectory()
.getAbsolutePath());
Try this for getting file path on sdcard:
String filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "Letsmeet/letsmeet_media/group_images/" + gridImage[position];
use this code:may help you..
Bitmap mybit=BitmapFactory.decodeFile(file1.getAbsolutePath());
image.setImageBitmap(mybit);
used to store your image in file format and get the image from that path.
Look at this Get filename and path from uri from mediastore. The answer provided by Nikolay Nikiforchuk is the way to go. And that does not only work with the mediafile but will also work good with all the files stored on SDCard
just replace this line
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
with this one
int column_index = cursor.getColumnIndexOrThrow("_data");
That would work fine.
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);