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.
Related
I am a little new to android and have been trying to send data to server using retrofit 2.What i am doing is selecting images from gallery and getting their paths but i have a less knowledge of how uploading works.
I have multiple images uri from gallery and using this function i get their paths
public String getImagePath(Uri uri) {
String selectedImagePath;
// 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
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();
selectedImagePath = cursor.getString(column_index);
} else {
selectedImagePath = null;
}
if (selectedImagePath == null) {
selectedImagePath = uri.getPath();
}
return selectedImagePath;
}
this gives me the paths like this.
for example: storage/emulated/0/Download/cheque_image.jpeg
do i need only these paths to create files
File file = new File(getimagepath(uri));
RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
Does the file created from path is enough to be uploaded using multipart or should inputstream from uri is used to be passed inside file and then to upload.
OutputStream outputStream = new FileOutputStream(file);
I am trying to make an android camera app with image gallery. The images captured are saved to a private directory: Android/data/com.example.newcamera/files/pictures.
Whenever I am using INTERNAL_CONTENT_URI or, EXTERNAL_CONTENT_URI as Uri, The app is bringing all the public pictures of my phone but not the one in the private directory. But I need only those with private directory. How can I get it? Please help me. My code snippet is as follows:
Thanks in advance.
protected String doInBackground(String... args) {
String xml = "";
String path = null;
String album = null;
String timestamp = null;
String countPhoto = null;
Uri uriInternal = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
Uri uriExternal = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri myUri = Uri.fromFile(new File(getApplicationContext().getFilesDir().getAbsolutePath()));
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.DATE_MODIFIED };
Cursor cursorExternal = getContentResolver().query(uriExternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor cursorInternal = getContentResolver().query(uriInternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor myCursor = getContentResolver().query(myUri, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal, cursorInternal, myCursor});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
timestamp = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED));
countPhoto = Function.getCount(getApplicationContext(), album);
albumList.add(Function.mappingInbox(album, path, timestamp, Function.converToTime(timestamp), countPhoto));
}
cursor.close();
Collections.sort(albumList, new MapComparator(Function.KEY_TIMESTAMP, "dsc")); // Arranging photo album by timestamp decending
return xml;
}
You can fetch your files from particular folder by:
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Folder Name/");
folder.mkdirs();
File[] allFiles = folder.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png"));
}
});
You can convert file path to Uri by Uri.fromFile(YOUR FILE)
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();
}
}
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
I am starting an intent to pick a file, I do pick a file and I get a URI, which I obviously can't use.
I am trying to convert this Uri to a legit file path and for that I use this method
public string GetPathToImage(global::Android.Net.Uri uri)
{
string path = null;
// The projection contains the columns we want to return in our query.
string[] projection = new[] { global::Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };
using (global::Android.Database.ICursor cursor = ManagedQuery(uri, projection, null, null, null))
{
if (cursor != null)
{
int columnIndex = cursor.GetColumnIndexOrThrow(global::Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
}
return path;
}
Problem: this method returns a null string. But I need a working one.
Any suggestions?