cant get file path from URI - android

I am trying to open a intent that lets me choose a file. Im able to select a file but when I try creating a file with the Uri I got in the OnActivityResult method I get a file size of 0. I dont think Im getting the right file path.
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/TESTAPP4");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri data = Uri.fromFile(file);
String type = "*/*";
intent.setDataAndType(data, type);
startActivityForResult(intent, 12);
onActivityResult:
Uri u= data.getData();
File file = new File( u.getpath);
file.length() // give 0

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();
}
}
}
Get filename and path from URI from mediastore

Related

Find out the camera picture location

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

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.

Image path from URI

I am trying to get an Image file from the gallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
GET_IMAGE_FROM_GALLERY);
The message "Selecture Picture" is not shown as a Toast.
And in onActivityResult();
Uri selectedImageUri = data.getData(); //log shows proper URI
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,
projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
cursor.getString(column_index) returns Null.
I am testing it on Nexus 4.
EDIT:
Looks like this is a problem with Android 4.4, I have see other apps failing too.
Convert content:// URI to actual path in Android 4.4
Use this :
String selectedImagePath = null;
Uri selectedImageUri = data.getData();
Cursor cursor = activity.getContentResolver().query(
selectedImageUri, null, null, null, null);
if (cursor == null) {
selectedImagePath = selectedImageUri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
selectedImagePath = cursor.getString(idx);
}

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