I know how to pick photo from gallery and it always work.
The code to pick photo from gallery:
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQ_GALLERY);
And code to handle result in onActivityResult():
Uri uri = data.getData();
Cursor cursor = getContext().getContentResolver().query(uri, new String[] {
MediaStore.Images.Media.DATA,
}, null, null, null);
if (cursor == null) return false;
if (cursor.moveToFirst()) {
String path = cursor.getString(0);
if (path != null) {
startPhotoEdit(new File(path), output, requestCode);
cursor.close();
return true;
}
}
cursor.close();
This time I pick them from Google Photos and the photo is still stored in cloud and not downloaded to local yet. After I pick one and it starts downloading like that:
photo_downloading
When the download task finished and return to my application, the _data column in cursor which always contains photo path is null.
Can someone help me, please.
I finally find that when photo from cloud is picked, its Uri is different. Like this:
content://com.google.android.apps.photos.contentprovider/1/1/mediakey%3A%2Flocal%253A131b7978-cc48-4206-8561-d18f99421551/ORIGINAL/NONE/2134743478
It contains mediakey instead of content in normal Uri.
So, this might be helpful:
getContentResolver().openInputStream(uri);
Related
I'm trying to gather EXIF info (date of picture taken, geotagging, orientation) from images selected from an EXTERNAL_CONTENT_URI intent, but it seems that if the pictures come from the internet (e.g. Google Photos) the EXIF data is somehow truncated.
For example if I download a picture from a web browser from photos.google.com on my PC, its size is 4.377.104 bytes, and all the EXIF data are there.
While if I download the same exact image using the command:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
its size is 4.363.578 bytes (13526 bytes less than the original) and all the EXIF data are lost
Any idea on how to download the full original image?
PS: if I select a picture from the gallery which was taken from the phone and it is still resident on the phone's storage, the EXIF data are present
I finally found out a solution. after getting the source image Uri in the onActivityResult method using
Uri selectedImage = data.getData();
I use the following function to get the necessary data from the picture
public static ImageInfo getImageInfo(Context context, Uri photoUri) {
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] {
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.Images.ImageColumns.LATITUDE,
MediaStore.Images.ImageColumns.LONGITUDE,
MediaStore.Images.ImageColumns.DATE_TAKEN } , null, null, null);
if (cursor.getCount() != 1) {
return null;
}
cursor.moveToFirst();
ImageInfo i = new ImageInfo();
i.Orientation = cursor.getInt(0);
i.Lat = cursor.getDouble(1);
i.Lon = cursor.getDouble(2);
i.DateTakenUTC = cursor.getLong(3)/1000;
cursor.close();
return i;
}
I'm trying to get a video from the Android Gallery and get the full path, so I can upload the video in an app...
I'm displaying the gallery like this:
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
activity.startActivityForResult(Intent.createChooser(intent, context.getResources().getString(R.string.popup_menu_share_video_title)), Const.PICK_VIDEO_REQUEST);
I then extract the uri:
Uri selectedUri = data.getData();
String selectedPath = getRealPathFromURI(this, selectedUri);
And attempt to get the path:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Video.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
But I can't seem the get the path...
I can see all the videos if I do this:
public static void dumpVideos(Context context ) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.Media.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
vidsCount = c.getCount();
while (c.moveToNext()) {
Log.d("VIDEO", c.getString(0));
}
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}
So I'm thinking there must be some real simple step, that I'm missing...
I'm trying to get a video from the Android Gallery
First, there is no "Android Gallery". There are many apps that might be considered "gallery" apps. There are thousands of device models, and they will ship with a wide variety of "gallery" apps, in addition to those that the user installs.
Second, ACTION_GET_CONTENT is not somehow magically limited to "gallery" apps. Any app can elect to support ACTION_GET_CONTENT for video/*.
and get the full path
That is impossible. You have no way of knowing what app the user chooses to handle your ACTION_GET_CONTENT request. You have no way of knowing what sort of Uri you will get back. You have no way of knowing where the content identified by that Uri will be stored, as it does not have to be a file on the filesystem that you can access.
so I can upload the video in an app
Use openInputStream() on ContentResolver to get an InputStream on the content identified by the Uri. Then, either:
Use that stream directly with your preferred HTTP client API to upload the content, or
Use that stream to make a local file copy of the content, then use that file with your preferred HTTP client
And attempt to get the path:
At best, that code will work in very limited circumstances:
The app that the user chooses to handle your ACTION_GET_CONTENT request happens to return a content Uri from the MediaStore
That content happens to be on external storage, not removable storage
Since that will not cover all of your scenarios, get rid of that code.
I can see all the videos if I do this
No, you can get paths for some videos:
Not every video accessible via ACTION_GET_CONTENT will be known to the MediaStore
Not every video in the MediaStore will be on external storage, where you might be able to access it via filesystem APIs
you can solve this issue by using ACTION_PICK instead of ACTION_GET_CONTENT. I believe unless you need the functionality of adding pictures/videos from Dropbox etc. it's not worth it.If all you need is to get stuff from the gallery use this code:
if (Build.VERSION.SDK_INT < 19) {
final Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/* video/*");
startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
} else {
final Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("*/*");
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
}
I just tried the code you posted with ACTION_PICK and it works perfectly!
I'm trying to get real path from Uri(Of selected image from gallery) but This function returns always null value :
//Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
cursor.moveToFirst();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
That's because the image you're selecting is not physically available on the device.
Images from gallery or other choose can come from different sources that does not provide a physical file, like a cloud storage for example.
Check this code here on how to open the Uri as a InputStream that can be used to create a copy of the file (or just read directly).
Android: Getting a file URI from a content URI?
edit:
I'm doing some extra research on it, apparently it also varies on how you request this Uri.
if you request it like this (which is the current preferred method as per Android guides):
i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, REQUEST_STORAGE);
and it opens the KitKat style unified selector, if you choose an image from there, it will always return null. If you open the left-drawer menu, select the gallery (or a file browser), and pick an image from there, then, if it is a local file image, you will have the correct path.
on the other hand, if you request it like this (which is the old method):
i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i, REQUEST_STORAGE);
it will directly open the Gallery, and again, if it is a local file image, you will have the correct path.
So yeah, I still don't believe there's a way to force local only, but at least you have more information to make an informed decision on your coding.
Can I store the image URI in sql DB so that I can retrieve it later to show the image? Or does it aways has to be full absolute path?
Thank you
you sure can store it in the database, convert it to a string and convert it back when you want to use it
Yes you can.
For example if you pick/take image, you will start the picker/camera intent, and you will get the result in onActivityResult, here you should get the image uri.
If the user pick from the gallery, you should do the following to get the uri.
String imageUri = null;
Cursor cursor = mContext.getContentResolver().query(uri, new String[] {android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
if (cursor != null) {
imageUri = cursor.getString(0);
}
cursor.close();
Now, save imageUri
If the user take image from the camera;
File photoFile = new File("your directory", "name");
Intent takePicIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePicIntent, ACTION_TAKE_PHOTO);
Then, in onActivityResult
String imageUri = photoFile.getPath();
And save it!
I am working on an activity and associated tasks that allow users to select an image to use as their profile picture from the Gallery. Once the selection is made the image is uploaded to a web server via its API. I have this working regular images from the gallery. However, if the image selected is from a Picasa Web Album nothing is returned.
I have done a lot of debugging and narrowed the problem down to this method.
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
//cursor is null for picasa images
if(cursor!=null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
Picasa images return a null cursor. MediaStore.Images.Media.DATA is not null for them, however. It only returns an #id, so I am guessing that there is no actual bitmap data at the address. Are Picasa images stored locally on the device at all?
I also noticed from the documentation that MediaStore.Images.ImageColumns.PICASA_ID exists. This value exists for selected picasa images but not other gallery images. I was thinking I could use this value to get a URL for the image if it is not store locally but I can not find any information about it anywhere.
I have faced the exact same problem,
Finally the solution I found, was to launch an ACTION_GET_CONTENT intent instead of an ACTION_PICK, then make sure you provide a MediaStore.EXTRA_OUTPUT extra with an uri to a temporary file.
Here is the code to start the intent :
public class YourActivity extends Activity {
File mTempFile;
int REQUEST_CODE_CHOOSE_PICTURE = 1;
(...)
public showImagePicker() {
mTempFile = getFileStreamPath("yourTempFile");
mTempFile.getParentFile().mkdirs();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));
intent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
startActivityForResult(intent,REQUEST_CODE_CHOOSE_PICTURE);
}
(...)
}
You might need to mTempFile.createFile()
Then in onActivityResult, you will be able to get the image this way
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
case REQUEST_CODE_CHOOSE_PICTURE:
Uri imageUri = data.getData();
if (imageUri == null || imageUri.toString().length() == 0) {
imageUri = Uri.fromFile(mTempFile);
file = mTempFile;
}
if (file == null) {
//use your current method here, for compatibility as some other picture chooser might not handle extra_output
}
}
Hope this helps
Then you should delete your temporary file on finish (it is in internal storage as is, but you can use external storage, I guess it would be better).
Why are you using the managedQuery() method? That method is deprecated.
If you want to convert a Uri to a Bitmap object try this code:
public Bitmap getBitmap(Uri uri) {
Bitmap orgImage = null;
try {
orgImage = BitmapFactory.decodeStream(getApplicationContext().getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
// do something if you want
}
return orgImage;
}