Getting the ImageView image location - android

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.

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

Android downloading an image

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

Referencing a file on SD-Card, Android

I currently trying to carry out the same operation (OCR) on images in android, one option is to use the camera and the other is to load an image from the SD-CARD. The code works when taken from a camera where the code is
Bitmap bitmap = BitmapFactory.decodeFile(_path,options);
where _path is equal to the last image taken using the application ( _path = DATA_PATH + "ocr.jpg"; ) . However when I try to use an image selected from the gallery where _path would equal,
imageCaptureUri = data.getData();
_path = imageCaptureUri.getPath();
The program locks up with the error
Failure deleiving result ResultInfo{who=null, request = 2, result = -1, data=intent {dat=content://media/external/images/media/26 typ=image/jpeg(has extras)}} to activity{com.project.projectActivity}: java.lang.NullPointerException
If anybody has an idea of whats going on I'd like to hear from you !!
You can get path of the image as..
_path = getPath(imageCaptureUri);
public String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor
.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
return cursor.getString(column_index);
}
and then
Bitmap bitmap = BitmapFactory.decodeFile(_path,options);
Note : If you are cropping image then this method does not work in this case Gallery will generate cropped image on same directory of image which you are cropping.
The Gallery returns you an Uri to access the image.
You need to use the decodeStream method from BitmapFactory, and for that you need to open an InputStream on the Uri given :
InputStream is = getContentResolver().openInputStream(imageCaptureUri);
Bitmap bitmap = BitmapFactory.decodeStream(is, options);

Android get image path from drawable as string

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}

Categories

Resources