URI/URL Paths from Gallery and Camera-taken-photo are different - android

I am trying to get the users to select between taking a picture with the device default camera and select from the gallery of images also default to the device.
I can get the camera to take the picture and have it display within the app just fine since it seems to like the URI pathing straight to a JPG file. However, the pathing given to the gallery URI is very different and does not display the image at all.
Here are the pathes I get:
WHEN TAKEN FROM CAMERA:
/mnt/sdcard/filename.jpg
WHEN CHOSEN FROM GALLERY:
/external/images/media/# (# is the ID number/thumbnail I believe)
The code used for retrieving both pathes are:
CAMERA:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri =
Uri.fromFile(new file(Environment.getExternalStorageDirectory(),
"fname_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
GALLERY:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
With the Gallery, it opens and I can browse just fine, it just doesn't display the image as it does with taking the picture.
The code used for pulling the images into my app once selected/taken is:
ImageView getMyphoto = (ImageView) findViewById(R.id.usePhoto);
String stringUrl = prefSettings.getString("myPic", "");
Uri getIMG = Uri.parse(stringUrl);
getMyphoto.setImageURI(null);
getMyphoto.setImageURI(getIMG);

Check for the "/external" in the uri string and then use get the right path method to get the absolute path.
private String getRealPathFromURI(Uri contentUri) {
int columnIndex = 0;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
try {
columnIndex = cursor.getColumnIndexOrThrow
(MediaStore.Images.Media.DATA);
} catch (Exception e) {
Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI",
Toast.LENGTH_SHORT).show();
finish();
return null;
}
cursor.moveToFirst();
return cursor.getString(columnIndex);
}

Related

Android error intent pick image from gallery depending on path

I have a function which tries to get the image from the image path, it works depending on were the image is.
For some reason, it works fine from this location
/storage/extSdCard/DCIM/100MEDIA/IMAG0082.jpg
but these locations does not work
/storage/extSdCard/DCIM/Camera/20150315_133312.jpg
/storage/emulated/0/Pictures/20150825_161105.jpg
It results in a blank thumbnail and an error when i try to upload the image from file path.
Here is my Intent for picking the photo:
//Camera Application for existing Photo
public void btnExistingPhotoClicked(View v) {
//invoke the intent to get an image from the phone
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
}
And here is the actual onActivityResult which picks it up, put it in a thumbnail and then display the imagepath in a Textview
case 1:
if(resultCode == RESULT_OK){
//function for a selected image
Uri selectedImage = imageReturnedIntent.getData();
mImageView.setImageURI(selectedImage);
Toast.makeText(this, mImageView.toString(), Toast.LENGTH_LONG).show();
Log.d("MainActivity", mImageView.toString());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgurl = cursor.getString(columnIndex);
cursor.close();
//The imageview is set, this is the small preview in the app
TextView tximgurl = (TextView) findViewById(R.id.txtImgUrl);
tximgurl.setText(imgurl);
Log.d("MainActivity", imgurl.toString());
//Toast.makeText(this, imgurl, Toast.LENGTH_LONG).show();
}
break;

How to pick image or video on Android L?

I am using below code and it works fine below android 5. I am able to pick image or video from SD card.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/* image/*");
getActivity().startActivityForResult(photoPickerIntent, 1);
However on Android L it shows only videos.
tried searching but didn't find anything, any help will be appreciated.
hi #Mohit you can use this solution for image and video
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("*/*");
getActivity().startActivityForResult(photoPickerIntent, 1);
for both image and video you can use setType(*/*);
here ACTION_GET_CONTENT is give only gallery selection while ACTION_PICK give many more options to pick image and video from different action, So as per #DipeshDhakal answer you should use only ACTION_GET_CONTENT.
and this is work on android L and api 10 also.
Use Intent.ACTION_GET_CONTENT
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("video/*, images/*");
startActivityForResult(photoPickerIntent, 1);
Was running into a similar issue. Code that worked on 5.0 and below started breaking on 5.1+, and only filtered by the first type passed in.
A co-worker came up the following, and I thought I'd share:
We were previously using the following intent:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*,video/*");
and the following code to get the path from whatever the user selected, if anything:
public static String getFilePathFromURI(Uri imageUri, Activity context) {
String filePath = null;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(imageUri,
filePathColumn, null, null, null);
if (cursor != null) {
try {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
} finally {
cursor.close();
}
}
return filePath;
}
Now:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
And to get the path, the getPath function found in this answer:
https://stackoverflow.com/a/20559175/434400

Allowing User to select Image from camera and Gallery dose not work for installed app like Camera360

I'm newbie in Android development. Well my application allows the user to select images from a gallery and to captures image taken from camera. Well it works perfectly fine while picking an image from a gallery and Native Camera but it dose not work when picking an image from an installed app like Camera360. Can anyone help me with this issue.
Below is my code to show a options to select images from Gallery and camera
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator);
if (!root.exists())
root.mkdirs();
String fname = "img_" + System.currentTimeMillis() + ".jpg";
File sdImageMainDirectory = new File(root, fname);
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
List<Intent> cameraIntents = new ArrayList<Intent>();
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
String packageName = res.activityInfo.packageName;
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, PICK_IMAGE_REQUEST);
onActivityResult Method is implemeted below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE_REQUEST) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else
isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
Bitmap yourSelectedImage;
if (isCamera) {
yourSelectedImage = BitmapFactory.decodeFile(outputFileUri.getPath());
} else {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
com.pkmmte.view.CircularImageView CircularImageView = (com.pkmmte.view.CircularImageView) findViewById(R.id.profileImage);
CircularImageView.setImageBitmap(yourSelectedImage);
}
}
}
The code works perfectly fine for both camera and gallery image picker. But it crashes for installed app like Camera360. Can some one help me regarding this
A Uri is not a file, and so you cannot pass it to decodeFile() on BitmapFactory. Your "pretend this Uri actually came from MediaStore" code will not work either.
Either use an image loading library like Picasso or Universal Image Loader, or have your own background thread that uses openInputStream() on a ContentResolver to read in the contents of that Uri, passing the stream to decodeStream() on BitmapFactory.
The code works perfectly fine for both camera and gallery image picker.
Only for the couple of cases that you tried. For example, if the "gallery image picker" returns an image that is on removable storage on Android 4.4+, even if you could get a filesystem path, you can't read it, as you don't have read access for arbitrary locations on removable storage.

Video path is not accessing in android 4.4 when I am going to pic a video from gallery

Hello friends I am using the following code to pic a video from gallery..
private static final int SELECT_VIDEO = 3;
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select a video"), SELECT_VIDEO);
On activity result:
#Override
protected void onActivityResult( int requestCode, int resultCode, Intent data)
{
try{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO)
{
Uri selectedImageUri = data.getData();
videopath = getPath(selectedImageUri);
}
}
}catch(Exception e){
// MLog.e("On Activity result.", "Error: "+e);
}
}
//get path method
public String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION};
Cursor cursor = managedQuery(uri, projection, null, null, null);
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
return filePath;
}
Now using above code I am getting null for video path in android 4.4. Can any one help me and let me know what should I have to use that I can pic the video from gallery successfully in android 4.4.
Now using above code I am getting null for video path in android 4.4.
You will get null in many cases, but particularly on Android 4.4+. That is because there does not have to be a file that you can access that corresponds to the video. A Uri is not necessarily a File.
You need to get rid of getPath() and use the Uri properly. For example, MediaPlayer and VideoView can work with the Uri directly. If you need the bytes of the video, use openInputStream() on ContentProvider to read in the video.

how can i get real path in image gallery?

I got all images from device's gallery,but i got images path is like content://media/external/image/media/102,so i want to get real image path for each images and send email,how can i get path?given below code i used,anybody knows,please give some sample code for me..i have email code.i want to only get real image path.How can I convert this path to real one (just like '/sdcard/image.png') ?
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Uri photoUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = managedQuery(photoUri, proj,null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
it work well..

Categories

Resources