Real path of Android pictures from photo gallery - android

I would like to do image processing on pictures from the photo gallery.
So, I'm working on module that works on android Bitmap by reading an image file.
From my Titanium application, I open the photo gallery and send the event.media.file.nativePath to my module.
But, it seems that it can't find it because when I create a
File file = new File(path)
I get a file.exists() == false. The path I get from the media looks like this on my phone :
file:///content://........../data/data.........jpg
Is there a way to create an Android Bitmap from media of Ti.Media.openPhotoGallery ?

"file:///content://........../data/data.........jpg"
This file path seems to be like on BlackBerry. On you can get external storage like this:
Environment.getExternalStorageDirectory().getAbsolutePath();
Then you can add path to your file.

Please! try below sample code.
Uri selectedImageUri = **put your content url**;
String[] filePathColumn = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(
, filePathColumn, null, null, null);
.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);

Related

Android get the actual filepath from gallery or google photos

I am trying to get the actual path of an image so that I can upload or send to server as multipart/form-data. So, Once I get the Uri, I have below code
Uri selectedImageUri = data.getData();
File file = new File(selectedImageUri.getPath());//create path from uri
final String[] split = file.getPath().split(":");//split the path.
String path = split[1];//assign it to a string(your choice).
uri value = content://com.android.providers.media.documents/document/image%3A32
path value = 32
I don't get it why path is a number, even I tried using getcontentResolver() and with that too I am getting path =32. I am using API 29 so I can't use MediaStore.Images.Media.DATA so, I have to use MediaStore.Images.Media._ID. It would be good if someone can also tell how to upload image as form-data too once i have path.

Uploading pdf / docx files including images to server using retrofit in android

I am new to android development. I came across with a tutorial which enables me to upload images to PHP server using retrofit. However I cannot figure out the way to upload other file types. I just need to get the absolute file path (files like PDF and DOCS) by modifying the code mentioned below. I want to use MediaStore as the option as my entire project has already been set up.
/*
* This method is fetching the absolute path of the image file
* if you want to upload other kind of files like .pdf, .docx
* you need to make changes on this method only
* Rest part will be the same
* */
private String getRealPathFromURI(Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
}
use multipart feature of retrofit. refer this tutorial in case you are new to android retrofit multiparttutorial remember if you will going to have uploading and downloading operation then you must should have separate directory of different file which helps you to manage it.

Not getting all media files which inserted manullay in sd card

I used below query to load all pictures or video from sd card and it works accordingly. But when I add some video or pictures manually into sd card at different folder then its not loading after query. Please suggest me regarding same.
final String[] columns = { MediaStore.Video.Media.DATA,
MediaStore.Video.Media._ID};
final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
Uri videosuri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Cursor imagecursor = getContentResolver().query(videosuri, columns, null, null, orderBy);
if (imagecursor != null && imagecursor.getCount() > 0) {
while (imagecursor.moveToNext()) {
int video_id=imagecursor.getInt(imagecursor.getColumnIndex(MediaStore.Video.Media._ID));
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Video.Media.DATA);
String path=imagecursor.getString(dataColumnIndex);
}
}
When you "add some video or pictures manually into sd card at different folder", the MediaStore has to be updated in order for them to be available to your query, as well as to other apps that use the MediaStore backend.
Adding them via MTP or apps like Gallery will invoke the MediaScanner (or some similar process) to add them to the MediaStore, but adding them via adb push or in your own code requires you to explicitly do so afterward.
In your code, after writing an image or video file to the SDCard, you can pass the path and MIME type of the file to the MediaScanner by implementing the MediaScannerConnectionClient interface in a class, instantiating it, then calling scan(). The MediaScanner will then open the file, collect/generate metadata, and add the file to the MediaStore
See android - dynamically add pictures to gallery widget for an example class using this approach.

Getting Image path from URI not working for Picasa folder

Cursor cursor = contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaColumns.DATA));
Unlike other folders like Camera, Screenshots, Foursquare etc.., The
Pictures from Picasa folder have less no.of columns, of which _data is not
there
and Hence I get the Exception:
java.lang.IllegalArgumentException: column'_data' does not exist
There is a column named picasa_id and a Constant in android:
MediaStore.Images.Media.PICASA_ID
Will that help in getting the correct image path?
Also if I had to download the image from the folder and save in my-app-images folder and then get the actual path. How will I even know that the image URI is from Picasa?

how to update thumbnails preview of android gallery after image edited

possible duplicate
Refreshing the thumbnail using MediaScanner
I have a problem regarding the update of thumbnails in the android in built-in gallery. Actually I have opened, edited and saved the image which was selected in the gallery.
I open leave the gallery and open the gallery again, then the thumbnail for that image is not updated. However, I pick the image, it is the edited version indeed.
Does anybody have solution about this? I have search but I'm getting the answer about refreshing the gallery using sendBroadcast(), which does not seem to update the image previews.
Edit
I don't want to delete the existing file because suppose user want to save file with different name then that file did not affected
Two suggestions.
Try getThumbnail. From the javadoc, I would expect it to regenerate an up-to-date thumbnail.
If that doesn't update the thumbnail, try deleting the thumbnail.
I found a workaround for this problem, before you request a new thumbnail you need to delete old one. Use MediaStore and ContentResolver for this
private static void removeThumbnails(ContentResolver contentResolver, long photoId) {
Cursor thumbnails = contentResolver.query(Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails.IMAGE_ID + "=?", new String[]{String.valueOf(photoId)}, null);
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {
long thumbnailId = thumbnails.getLong(thumbnails.getColumnIndex(Thumbnails._ID));
String path = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.DATA));
File file = new File(path);
if (file.delete()) {
contentResolver.delete(Thumbnails.EXTERNAL_CONTENT_URI, Thumbnails._ID + "=?", new String[]{String.valueOf(thumbnailId)});
}
}
You can get photoId from its URI, to get URI from file name just create File and parse URI from it
Uri uri = Uri.fromFile(file);

Categories

Resources