Android error intent pick image from gallery depending on path - android

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;

Related

Android Select Multiple Images from gallery?

How can I select multiple images from gallery by using java.
I have Use this code but no error occurs
and can not able to select image from gallery.
Here is my code:
mainactivity.java
//button On Click:
{
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
//super.onActivityResult(requestCode,resultCode,data);
switch(requestCode) {
case 1234:
if(resultCode == RESULT_OK){
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();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
/* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */
}
}
}
});
It's not working. I can't select image.
#Drashti Kapadia :You can use Android open source library MultipleImagePick. And it uses Universal image loader library for asynchronous loading and caching.
Features and Benefits:
Select multiple images - Option to select multiple images from
Gallery and fast scroll over the Gallery
Maximum selection limit - Option to set maximum image selection
limit
Custom button title and error message - There is a option to
customize button title and error message
Method for scale down the bitmap - To avoid out of memory issue,
this module has in build bitmap scale down method
Callback methods - Success, error and cancel callback methods
Try the following code. Hope this helps!
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); // allow the user to select multiple items, only available in Android API 18 and higher.
startActivityForResult(intent, requestCode);

Selected Image content the correct path but cursor is null

I'm trying to display the photo just taken by camera and display it in a imageView. This is the method that has the problem:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = fileUri;//data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
//managedQuery(selectedImage,filePathColumn, null, null, null);//
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
// The following three lines work perfectly :-)
// if I comment the part of the cursor lines above
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}
}
When debugging I see some values like for example
selectedImage : file:///mnt/sdcard/external_sd/Pictures/MyCameraApp/IMG_20150530_032752.jpg
and I think this is ok. Another interesting value is
filePathColumn : _data
is this value an expected one? You tell me, please.
So, cursor is null and the line
cursor.moveToFirst();
spits the error null pointer :-/. I'm debugging code in real device with Android 2.2. Help.
EDITION
this is the method that call the previous one
private void clickpic() {
// Check Camera
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// Open default camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
/*
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
*/
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create
//the getOutputMediaFileUri is implemented as saving media file suggests by developer.android.com
//intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// start the image capture Intent
startActivityForResult(intent,100);
} else {
Toast.makeText(MainActivity.this, "Camera not supported", Toast.LENGTH_LONG).show();
}
}
The commented code are my failed attempts.
You need to put the fileUri as an extra as shown here Android Developers
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
and try replacing your onActivityResult with:
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = fileUri;
Bitmap photo = BitmapFactory.decodeFile(new File(selectedImage));
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}

display customized Phone gallery to pick multiple images

I am in trouble displaying customized phone gallery.
At first, I need to show images folders in gridview (such as phone gallery), and on selecting any of the folder, it must show the pictures inside it and should allow multiple selection, so that I can pick multiple pictures.
Is it achievable?
If yes, how to do that?
To open gallery :
//open gallery to choose image
private void captureImage() {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
When the image is selected from the gallery , the following function is invoked , implement it to save the image selected .
// When Image is selected from Gallery
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
// Get the Image's file name
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// successfully selected the image
// launching upload activity
tvCapturePicture.setText(fileName);
} else {
Toast.makeText(this, "You haven't picked any Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong ,please try again , ",
Toast.LENGTH_LONG).show();
}
}
as per your requirement follow this tutorial it will give ideas..
http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

How to display the mobile gallery page in a custom view in android? [duplicate]

This question already has answers here:
populate gallery with images from web
(2 answers)
Closed 7 months ago.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
The above will take us to Gallery application which is there in our device,so we will be getting the Gallery screen which occupies the whole display of our device.But I want to display that Gallery application view in a Small Layout(something like I shown in below picture,instead of EditText,Buttons ,I want to get My device Gallery screen in that small layout).So that I can give user a feeling that he is not moving out of application.
I have seen this in one of the IPAD app (HelloAlbums).
IS it possible to achieve this in android?
I am using the above code to call phone gallery application.
But I want to display the gallery page in a custom view.
Suppose like in a view which I used below.
How can I achieve this?
please suggest
Thanks
try this
public void ChoosePicture(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bMap_image = BitmapFactory.decodeFile(filePath);
ImageView img = (ImageView) findViewById(R.id.gallery1);
img.setImageBitmap(bMap_image);
}catch(Exception e)
{}
}
}// resultCode
}// case 1
}// switch, request code
}

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

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

Categories

Resources