Intent extras are null when picking an image from gallery - android

I have been researching on this but I am not able to find an answer for this.
I am picking an image from the gallery using media store intent and I am able to get the image file path in onActivityResult method. (I know how to get the URI in the intent and filepath).
I am passing in some intent extras on starting the activity (startActivityForResult) but all the intent extras are null.
Code snippets (in case):
This is my onActivityResult code which is working and i get the image path
/* On activity result from image button */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Result Code" + resultCode);
if(requestCode == FavoriteListAdapter.IMAGE_PICK_CODE && data != null && data.getData() != null && resultCode == FragmentActivity.RESULT_OK) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
String imageFilePath = cursor.getString(0);
System.out.println("imagefilepath" + imageFilePath);
System.out.println(data.getStringExtra("exp"));
cursor.close();
}
}
I am starting my activity with startActivityForResult
Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageIntent.setType("image/*");
imageIntent.putExtra("exp", "testing");
((FragmentActivity)view.getContext()).startActivityForResult(imageIntent, IMAGE_PICK_CODE);
I should be able to get the string "testing" in onActivityForResult but all I get is null.
Any ideas and suggestions will be appreciated. THnkas a lot.

Actually I figured it out .. When you are sending an intent to a system activity like MediaStore or the camera etc... the onActivityResult will not have the intent extras you sent while calling the activity.
This is probably by design and will only contain the extras given by the system activity. For instance after picking an image from the gallery, the returning intent from the gallery will only the URI containing the image path.
Same goes to camera or any system activites.

Related

How to open an Image without saving another Location?

now file:// uri are not supported. people say to use FileProvider but i think i have to save image in app data location to use this. is there a way to get content:// URI Without Saving image in data directory.i want to open image in sd card from gallery. this below code works on old phones, after android 7 it is not working. FileUriExposedException example image file is in dcim folder my old code is this
File pic2toview = new File(imagepath);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(pic2toview), "image/*");
startActivity(i);
Code to select an image from gallery by intent,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST);
Then it handled in onActivityResult,
#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) {
imageUrl = data.getData();
}
}
Here imageUrl is Uri of image selected from gallery.

How to get non-null data out of Intent on file selection?

I would like to let user select an audio file, and then I would like to get the file path of it. However I have even simpler problem, because Uri of the intent I get is null. How I could get something meaningful, i.e. non-null?
Starting intent:
private void MainActivity_Click1(object sender, System.EventArgs e)
{
//var intent = new Intent();
var intent = new Intent(Intent.ActionPick, Android.Provider.MediaStore.Audio.Media.ExternalContentUri);
intent.SetType("audio/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(
Intent.CreateChooser(intent, "Select audio file"), SelectAudioCode);
}
And responding to the selection:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok && requestCode==SelectAudioCode)
{
Android.Net.Uri uri = Intent.Data;
audioFileNameText.Text = uri == null ? "<NULL>" : uri.ToString();
}
}
No matter how I create the intent (see the code above), I see always NULL.
Update: if I use the Intent passed via parameter, the Data property of it is null as well.
I would like to let user select an audio file, and then I would like to get the file path of it. However I have even simpler problem, because Uri of the intent I get is null.
You can get the content Uri from data.Data. But if you want to get the file path out of content uri, you need to convert the content uri into real path:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
string abc= getRealPathFromURI(this,data.Data);
}
public string getRealPathFromURI(Context context, Uri contentUri)
{
ICursor cursor = null;
try
{
string[] proj = { MediaStore.Audio.AudioColumns.Data};
cursor = ContentResolver.Query(contentUri, proj, null, null, null);
int column_index = cursor.GetColumnIndexOrThrow(MediaStore.Audio.AudioColumns.Data);
cursor.MoveToFirst();
return cursor.GetString(column_index);
}
finally
{
if (cursor != null)
{
cursor.Close();
}
}
}
Explaination Update:
The intent you are currently using by Intent.Data is the intent that start current activity, not the intent that chooser returned back:
So instead of using Intent.Data you need to use the intent returned by the chooser activity, which is data argument:
Looks like you are using Xamarin. The Java equivalent of what you are trying to achieve is this.
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setType("audio/*");
this.startActivityForResult(intent,RESULT_CODE);

Android camera intent file into imageview NPE (SGS3)

There's lots of questions on the same topic, however none of them provided me an answer.
I've tried a lot of solutions, yet none of them is working for me so far.
I'm invoking the camera intent in a fragment and inserting uri to the newly created file where I want the picture to be stored and then on activityresult I'm passing the uri to a new activity where I want to show it in an imageview and then proceed to upload it.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mImageFile = new File(Environment.getExternalStorageDirectory().getPath()+"/DCIM/" + "Camera/" + File.separator+System.currentTimeMillis()+".jpg");
imageUri = Uri.fromFile(mImageFile);
Log.d("camera", "imageUri: " + imageUri.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, CAMERACODE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case CAMERACODE:{
if (resultCode == Activity.RESULT_OK) {
Intent intent = new Intent(getActivity(), SubmitActivity.class);
intent.putExtra("imageuri", imageUri.toString());
getActivity().startActivity(intent);
and in SubmitActivity:
Bundle extras = getIntent().getExtras();
imageUri = extras.getString("imageuri"); <-- imageUri has the correct path, and the actual file is created and the photo is there. The photo is not showing up on gallery for some reason though, only when browsing the folders. What is the reason for this?
iv = (ImageView) findViewById(R.id.selectedpicture);
iv.setImageURI(Uri.parse(imageUri)); <-- this line causes a NullPointerException.
Can anyone explain the reason for why the setImageURI fails? Any alternative way of doing this? I'm using Samsung Galaxy S3
I believe iv is null and there is no problem with imageuri.
Check whether you have done setContentView and check if the layout has selectedpicture imageView.
Edit:
The photo is not showing up on gallery for some reason though
Gallery uses MediaStore to get all the photos on the sdcard. But when you take a picture MediaStore db would not have been updated. For the pic to show up in gallery MediaScanning should be run. Check this post

take picture from android camera and send it to web server

im build some apps that call camera activity..
im just to take picture from my apps and send it to web server..
but i can't get my image path..
im always getting NullException Error when try to get image path..
here's my code when calling camera activity :
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(camera, PICTURE_RESULT);
and this is code for activity result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICTURE_RESULT){
if (resultCode == Activity.RESULT_OK) {
takePicture(data);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
protected void takePicture(Intent data) {
Bundle b = data.getExtras();
pic = (Bitmap) b.get("data");
if (pic != null) {
imagePicture.setImageBitmap(pic);
}
}
is there something wrong with my code?
Thanks
Ok, I see your problem. You're not setting the path to begin with. Please look at this doc.
http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
So you see, when you call ACTION_IMAGE_CAPTURE you are not passing it the extra EXTRA_OUTPUT that tells the application where the picture is going to be stored. This EXTRA_OUTPUT is the path to the file.
So right under where you make the intent do this:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI pictureUri = Uri.fromFile(new File(<path to your file>));
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

Accessing Android native gallery via an intent

How can I get a user to select from the native gallery in Android, rather than other gallery-like applications such as ASTRO File Manager?
The following code gives a list of activities that can select an image:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
List<ResolveInfo> infos = activity.getPackageManager().queryIntentActivities(intent, 0);
If I then do something like this:
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CHOOSE_IMAGE);
then if the user has more than one application that can act like a gallery (such as ASTRO File Manager), s/he is prompted to select one of them, with no "set as default" option.
I don't want the user to be prompted to choose between them each time, so I'd like to just use the native gallery.
The hacky code sample below uses a whitelist to test for known native gallery names:
for (ResolveInfo info : infos) {
if ( 0==info.activityInfo.name.compareTo("com.cooliris.media.Gallery")
|| 0==info.activityInfo.name.compareTo("com.htc.album.CollectionsActivity")
) {
// found the native gallery
doSomethingWithNativeGallery();
}
}
Feels kinda dirty. Is there a better way? I suspect I'm missing something in my intent.
I didn't get last part of your code.
To start the native gallry what i did is -
public void upload(){
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/jpg");
photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
startActivityForResult(photoPickerIntent, 1);
}
Call the upload() where you like to start the native gallery.
Then to get that image info i did -
/**
* Retrieves the returned image from the Intent, inserts it into the MediaStore, which
* automatically saves a thumbnail. Then assigns the thumbnail to the ImageView.
* #param requestCode is the sub-activity code
* #param resultCode specifies whether the activity was cancelled or not
* #param intent is the data packet passed back from the sub-activity
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_CANCELED) {
return;
}
else if(resultCode == RESULT_OK) {
Uri uri = intent.getData();
String path = getPath(uri);
Log.i("PATH", path);
data = path;
return;
}
uplad();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
If you wouldn't get your answer, clear what you like to do

Categories

Resources