Find out the camera picture location - android

Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"
I tried this code for find the location of camera storage images but its give only "DCIM" image location.but some devices not store always in "DCIM" folder so how could i find out storage location?

Try this:
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

Try to below code work for my app perfect :-
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
Method:-
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}

Related

Cursor returns null when getting filepath from URI

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
cursor.getString(columnIndex) Returns Null
Where as the selectedImage is not null.
Any suggestion on how I can create file from URI?
Cursor returns null when getting filepath from URI
A Uri is not a file. A Uri does not have to point to a file, let alone a file on the filesystem that you can access.
Any suggestion on how I can create file from URI?
Use ContentResolver and openInputStream() to get an InputStream on the content identified the Uri. Ideally, just use that InputStream directly. If, for some reason, you truly need a file:
Create a FileOutputStream on some file that you control (e.g., in getCacheDir())
Copy the bytes from the InputStream to the FileOutputStream
Use the file that you just created, deleting it when you are done
Use this method to get path:
public String getPath(Uri uri) {
// just some safety built in
if (uri == null) {
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
And in onACtivityResult use this:
Uri selectedImage = data.getData();

get real path from android URI after selecting image from gallery

I am ( a beginner android) trying to get the real path (ex: image_name.jpg etc) of Image selected from the gallery by the user.
Here is the code snippet:
if (requestCode == SELECT_FILE) {
Uri selectedImage = data.getData();
Bitmap bitmapImage;
try {
bitmapImage =decodeBitmap(selectedImage );
photoImage = (ImageView) findViewById(R.id.photoImage);
photoImage.setImageBitmap(bitmapImage);
photoName = (TextView) findViewById(R.id.designPhoto);
File thePath = new File(getRealPathFromURI(selectedImage));
photoName.setText(getRealPathFromURI(selectedImage));
} catch (Exception e) {
e.printStackTrace();
}
}
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}
but I am not getting anything in the photoName textView also tried lots of codes and guides but with no sucess.
but when I tried it with
photoName.setText(selectedImage.getPath());
I am getting
/document/image:n (where n=any numbers) ex:/document/image:147
but I want a proper file name or path ex: image_name.jpg or /path/image_name.png
trying it from last 3 hours, it would be great if anyone could help me out. humble Thanks in advance.
I got it working using
MediaStore.Images.Media.DISPLAY_NAME
updated and working code might help others:
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}

Android how to get the only uri of Media Store Image?

I'm trying to get the uri and after parse into path of MediaStore image.
I do this to get the uri:
Uri img_uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
And after with this function i convert uri in the path:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
But the result is that i have the path of the Media storage but with the first element.
For example: /mnt/sdcard/Pictures/Boat.jpg. But i want only /mnt/sdcard/Pictures/.
Please don't tell me to use join and split because isn't what i want.
Use this:
String path = getExternalFilesDir(null).getAbsolutePath();
to get the main directory in your app subfolder where you can put another directory, for example "Images" in this way:
String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
File folder = new File(path);
if (!folder.exists()) {
folder.mkdir();
}
So now with this:
String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
you can access to this folder without problems.

How to get file data from its uri in android

How to get file data from its uri in android.We tried following code but its giving filenot found exception.
uri2 = intent.getData();
uri = uri2.toString();
File objFile = new File(uri);
try {
InputStream data = new FileInputStream(objFile);
Log.d("data", data+"");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Please do help.
Thanks,
AA.
You can get the file path with the following piece of code :
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
call the above function as follows :
String myFilePath = getPath(uri);
File objFile = new File(myFilePath);

How to get the path of the image downloaded from third party application and stored in sd card

I am trying to open the gallery and select the image from there.I got the path of all the image which are captured from camera but cannot get the real path of the image which were downloaded from Facebook/picassa etc.The path it is giving is like https://lh3.googleusercontent.com/XNzSBp0MycQ/TigFxMIWn2I/AAAAAAAAAAg/YJPWAWmGOy0/I/11%252520-%2525201.jpg even though it is in gallery.
Here is my code ::
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
i am using the following code to get the path
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
public String getPath(Uri uri) {
int columnIndex = 0;
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String imagePath = cursor.getString(columnIndex);
return imagePath;
} else {
return null;
}
String filename = "image.jpg";
String path = "/mnt/sdcard/" + filename;
File f = new File(path); //
Uri imageUri = Uri.fromFile(f);

Categories

Resources