Gallery intent.getData() returns null in onActivityResult - android

I recently fixed a known bug in my app that occurs on some devices; when the user takes a photo from an intent launched from my app, in the onActivityResult Uri uri = intent.getData(); returns null. I managed to fix that in the suggested manner. However I get the exact same issue when the user needs to Select a picture from his phone's photo gallery. The same intent.getData() == null.
Starting the intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
onActivityResult:
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();
dataHasChanged(ACTION_PICTURE, filePath);
PS: Strangely enough if I start the intent like shown below I can get the intent.getData() but only if I use the "Gallery" app on my samsung s4 and not the GooglePhotos app.
Intent pickImageIntent = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (pickImageIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(pickImageIntent, R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
I am unsure on how to proceed. I find the entire Android intents affair very confusing sometimes.

I'm giving you code for your reference:
Use following code after clicking pic from your app:
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
Below code will be in your onActivityResult():
if (requestCode == PICK_FROM_GALLERY) {
if (resultCode != RESULT_CANCELED) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
photo = extras2.getParcelable("data");
bitmap = photo;
profile_imageView.setImageBitmap(photo);
new ImageUploadTask().execute();
}
}
}
Hope this will solve your problem.

static final int REQUEST_GALLERY_IMAGE = 14;
----------
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_GALLERY_IMAGE );
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY_IMAGE && resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imagePreview.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

Related

How to pick an image from gallery in samsung

I want to build an app can choose photo from the gallery. this is the code i use
Intent photo_pick = new Intent(Intent.ACTION_PICK);
photo_pick.setType("image/*");
startActivityForResult(photo_pick , PICK_PHOTO_INTENT );
this code i had try and it work on XiaoMi, Huawei phone. But when it work on samsung the path it return is the error path cannot to get the photo.
How to improve it to let the samsung phone also can work?
Check Below code for choose photo from the gallery,
private static final int REQUEST_PROFILE_ALBUM = 1;
Intent int_album = new Intent(Intent.ACTION_PICK);
int_album.setType("image/*");
int_album.putExtra(MediaStore.EXTRA_OUTPUT, img_url);
startActivityForResult(int_album, REQUEST_PROFILE_ALBUM);
After Select Image onActivityResult is called,
if (requestCode == REQUEST_PROFILE_ALBUM && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.getContentResolver().query(selectedImage, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String picturePath = cursor.getString(columnIndex);
}
Try this it may helps you:
Button btn_selectimage = (Button) findViewById(R.id.btn_selectimage);
btn_selectimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
img_capture.setVisibility(View.VISIBLE);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
Code for xml:
<Button
android:id="#+id/btn_selectimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Picture"/>
Pick a image like this
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
Once you pick a image onActivityResult is called automatically
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
}
}
try this code:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (intent.resolveActivity(getPackageManager()) != null) {
// Bring up gallery to select a photo
startActivityForResult(intent, 2);
}else{
Toast.makeText(UserProfileActivity.this,"No Gallery app found!", Toast.LENGTH_SHORT).show();
}
In order to execute below code, you will require to get request persmission only if android version is higher then lolipop
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 500);

Gallery Intent Not Working

I am experiencing odd behaviors. I am trying to bring up the gallery using an Intent. And then onActivityResult() I want to take the Uri and convert it to a Bitmap. The issue I am facing is that selecting an image first causes my phone to freeze for several seconds. And then by the time it returns to onActivityResult() the resultCode value is -1, even though I selected an image. Am I missing something in my steps?
Launch Gallery Intent
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_REQUEST_CODE);
My onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE) {
Uri selectedImageUri = data.getData();
if (selectedImageUri != null) {
try {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(
selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap mBitmap = BitmapFactory.decodeFile(filePath);
LogUtil.e(TAG, "mBitmap: " + mBitmap);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
Intent intent = new Intent(CameraActivity.this,
SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("image", bs.toByteArray());
startActivity(intent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
return;
}
}
Update:
I have permission in manifest. I am receiving the correct Uri too. But after selecting an image I am always receiving resultCode = -1. It seems that I am receiving a failed binder transaction.
Am I missing any permissions maybe? Thank you in advance!
to make this code work you need to have, write permission check if you have this in your manifest.
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

How do I get image to display when tapping various markers taken by camera intent?

I am trying to display a full size image of the marker (thumbnail) of the image taken by the camera intent on that point the users has tapped. But so far the image is displayed but always show the latest image taken and not the one taken before when tapping on the first marker.
Here is my code so far:
public void onMapLongClick(final LatLng point) {
poinOnMap=point;
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri selectedImage = intent.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 fileUri = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(fileUri);
Uri imgUri = Uri.parse("file://" + bitmap + fileUri);
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
return;
}
});
getApplicationContext().getDir(
getResources().getString(R.string.app_name), MODE_PRIVATE);
fileUri = Uri.fromFile(new File((Environment.getExternalStorageDirectory() +
"/" +getResources().getString(R.string.app_name)),new Date().getTime() + ".jpg"));
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent,TAKE_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Could anyone please tell me what I am doing wrong?
Uri selectedImage is null
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri selectedImage = intent.getData();

Force certain app to receive the intent (not chooser) [duplicate]

I'm looking for a way to open the Android gallery application from an intent.
I do not want to return a picture, but rather just open the gallery to allow the user to use it as if they selected it from the launcher (View pictures/folders).
I have tried to do the following:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
However this is causing the application to close once I select a picture (I know this is because of the ACTION_GET_CONTENT), but I need to just open the gallery.
Any help would be great.
Thanks
This is what you need:
ACTION_VIEW
Change your code to:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I hope this will help you. This code works for me.
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
ResultCode is used for updating the selected image to Gallery View
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
if (cursor == null || cursor.getCount() < 1) {
return; // no cursor or no record. DO YOUR ERROR HANDLING
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex < 0) // no column index
return; // DO YOUR ERROR HANDLING
String picturePath = cursor.getString(columnIndex);
cursor.close(); // close cursor
photo = decodeFilePath(picturePath.toString());
List<Bitmap> bitmap = new ArrayList<Bitmap>();
bitmap.add(photo);
ImageAdapter imageAdapter = new ImageAdapter(
AddIncidentScreen.this, bitmap);
imageAdapter.notifyDataSetChanged();
newTagImage.setAdapter(imageAdapter);
}
You can open gallery using following intent:
public static final int RESULT_GALLERY = 0;
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
If you want to get result URI or do anything else use this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case QuestionEntryView.RESULT_GALLERY :
if (null != data) {
imageUri = data.getData();
//Do whatever that you desire here. or leave this blank
}
break;
default:
break;
}
}
Tested on Android 4.0+
Following can be used in Activity or Fragment.
private File mCurrentPhoto;
Add permissions
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
Add Intents to open "image-selector" and "photo-capture"
//A system-based view to select photos.
private void dispatchPhotoSelectionIntent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECTOR);
}
//Open system camera application to capture a photo.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(App.Instance.getPackageManager()) != null) {
try {
createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (mCurrentPhoto != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhoto));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
Add handling when getting photo.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_IMAGE_SELECTOR:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null);
if (cursor == null || cursor.getCount() < 1) {
mCurrentPhoto = null;
break;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex < 0) { // no column index
mCurrentPhoto = null;
break;
}
mCurrentPhoto = new File(cursor.getString(columnIndex));
cursor.close();
} else {
mCurrentPhoto = null;
}
break;
case REQUEST_IMAGE_CAPTURE:
if (resultCode != Activity.RESULT_OK) {
mCurrentPhoto = null;
}
break;
}
if (mCurrentPhoto != null) {
ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv);
Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView);
}
super.onActivityResult(requestCode, resultCode, data);
}
As you don't want a result to return, try following simple code.
Intent i=new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivity(i);
if somebody still getting the error, even after adding the following code
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
then you might be opening the intent in another class (by calling) due to this the app will crash when you click the button.
SOLUTION FOR THE PROBLEM
Put intent in the class file which is connected to the button's layout file.
for example- activity_main.xml contains the button and it is connected to MainActivity.java file. but due to fragmentation, you are defining the intent in some other class(lets says Activity.java) then your app will crash unless you place the intent in MainActivity.java file. I was getting this error and it is resolved by this.

how to open the gallery?

I need to open the images gallery via code in my app.(only opening the gallery, the user is not going to select any image). I searched and found lots of ways but some of them worked only for selecting an image and other ways never worked.e.g.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
or
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent);
how may I open the gallery?
public void pickPhoto(View view)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
Here:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
Uri curImageURI = data.getData();
Bitmap bit = getRealPathFromURI(curImageURI);
imageView.setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
and
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
android.database.Cursor cursor = managedQuery(contentUri, proj, null,
null, null);
int column_index;
try {
column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return null;
}
}
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is the final number in the "startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);"
in this case '1' so you'd need to change it to 1 or declare it as a global variable

Categories

Resources