Beautiful way to come over bug with ACTION_IMAGE_CAPTURE - java

I faced problem: ACTION_IMAGE_CAPTURE intent's behaviour depends on hardware manufacturer.
I think, best way to get photo from camera inserted in photo gallery must be something like following
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, CAPTURE_IMAGE_REQUEST);
and then get uri in onActivityResult:
switch (requestCode) {
case CAPTURE_IMAGE_REQUEST: {
if(resultCode == Activity.RESULT_OK) {
Uri uri = intent.getData();// content uri of photo in media gallery
//do something with this
}
break;
}
But I see, that this doesn't work on many devices; moreover, I found several different scenarios of Camera app behaviour:
some devices have bug with this event, so there is no way to get fullsized photos, and you can get 512px wide photo using tmp file in public directory only
some devices (including mine) insert taken photo into gallery, but does not return Uri. (getData() returns null, intent extras have only boolean key 'specify-data', value = true) If I try to get photo through the public tempfile then photo will be inserted into both gallery and tempfile.
some devices don't insert taken photos to gallery - and I must do it manually
I dont know, but there can be other different scenarious
So, is there best practices in managing such problems to cover a wide range of devices and manufacturers?
In this case I need take photo from camera, get it inserted into gallery, then get uri of photo in gallery.

A part of this has already been answered :
Check the implementation of hasImageCaptureBug()
For the null problem, maybe you can watch over the Gallery folder?
I don't think you should be checking for other cases. Google and Android OEM's monitor those issues so report them and hopefully they will be fixed.

Related

Android Studio 1.0 - Using stock camera app and customizing file name after picture

Ok, so this code works great to call the stock android camera app:
public void clickEvent(View e) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
However, I need to capture that picture file after it's taken, rename it and maybe place it somewhere else.
Is there a way to do that?
You can set the EXTRA_OUTPUT extra on the intent to tell whatever app responds to the intent where to put the file.

Android Getting null image data

I am trying to get the image from the camera. It works fine. I can take a photo and show it on the image view. Actually, I want to send this photo to my server after took. To do that, I try to pull the image in onActivityResult. But, when i check the Intent data, it always return null.Even though, the application runs fine and display the image. Why am I getting null for Intent data? Could you please help me?
Here is the code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTION_TAKE_PHOTO_B: {
if (resultCode == RESULT_OK) {
Log.e("TAG","data: "+data);
display_Photo();
//Process the image to send but, data is null
}
break;
}
}
}
Log cat:
data : Null
In the code you are using there are two button onclick handlers that call:
dispatchTakePictureIntent(int actionCode)
One button calls it with the enum ACTION_TAKE_PHOTO_B while the other call sit with the enum ACTION_TAKE_PHOTO_S
If you are passing in ACTION_TAKE_PHOTO_B, then the expected result of data in the returned intent is null.
The reason why is that before dispatchTakePictureIntent calls the intent MediaStore.ACTION_IMAGE_CAPTURE, it sets an extra intent parameter based on the actionCode passed in:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
According to the documentation for ACTION_IMAGE_CAPTURE:
The caller may pass an extra EXTRA_OUTPUT to control where this image
will be written. If the EXTRA_OUTPUT is not present, then a small
sized image is returned as a Bitmap object in the extra field.
So, because the EXTRA_OUTPUT parameter is being set, your image is being written to disk instead of being returned as data in the intent. To get the file location, you can inspect mCurrentPhotoPath which is written to before the intent is launched.

capture audio from google recognizer intent [duplicate]

I am working on application that will record the voice of the user and save the file on the SD card and then allow the user to listen to the audio again.
I am able to allow the user to record his voice using the RecognizerIntent, but I cant figure out how to save the audio file and allow the user to hear the audio. I would appreciate it if someone could help me out. I have displayed my code below:
// Setting up the onClickListener for Audio Button
attachVoice = (Button) findViewById(R.id.AttachVoice_questionandanswer);
attachVoice.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Speak");
startActivityForResult(voiceIntent, VOICE_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == VOICE_REQUEST && resultCode == RESULT_OK){
}
There is an example of how to do audio capture using MediaRecorder in the Android Developer Documentation.
I would recommend saving the files on the SD Card and then have your gallery code check the SD card to see which files to display. You can get the directory of the SD Card using the Environment.getExternalStorageDirectory() method. It would be best to save your files in a subdirectory of the SD Card root directory.
Make sure you give your applications the Permissions it will need. At the very least it will need RECORD_AUDIO and WRITE_EXTERNAL_STORAGE.
Also you have to see these tutorials:
http://www.androiddevblog.net/android/android-audio-recording-part-1
http://www.androiddevblog.net/android/android-audio-recording-part-2
If you really want to record audio via the speech recognition API then you could use the RecognitionService.Callback which has a method
void bufferReceived(byte[] buffer)
This gives you access to the recorded audio buffer as speech is being recorded and recognized. (No information is provided about the sample rate though.) You can then save the obtained buffers into a file for a later playback. I think keyboard apps use this call to display the waveform of the recorded speech. You have to implement the UI yourself.
The bare RecognizerIntent.ACTION_RECOGNIZE_SPEECH just returns a set of words/phrases without any audio.

Camera control inside an Android application

I am building an application for Android 2.2 which is based on a photo library. There is an option to take picture from camera and use it in the application. I am expecting:
To enable the camera from the application.
To take the picture.
To automatically close the camera and show the captured picture inside the application
Unfortunately, I am not able to get the captured image to the application. Once photo is taken, camera is not closing automatically or returning to the app. Now I have to click the back button to go to the app and select the picture manually select from SD card. Camera is opening through intent and I am using the following class.
http://developer.android.com/reference/android/hardware/Camera.html
You need to do two things. Start the Camera app and tell it where to store the picture that it takes:
File photo = new File(Environment.getExternalStorageDirectory(), "myFile.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, myRequestCode);
When the user closes the Camera app, you app and Activity will be resumed. So you must override onActivityResult to get the result:
if (resultCode == myRequestCode){
File photo = new File(Environment.getExternalStorageDirectory(), "myFile.jpg");
// open it, show it, insert into MediaStore whatever
}
If you don't provide the place to save, you can alternatively retrieve it using either intent.getData or intent.getParcelableExtra using Intent.EXTRA_STREAM.
I guess .. you are doing similar to the below code
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
using startActivityForResult instead of startActivity.. whenever you are done with your camera.. press ok.. and you will come back to your activity.. then in your activity onActivityResult callback will be called. here you will get the data for the clicked image.
Thanks.

Debugging Intents

I asked a question previously about what shows up in the IntentChooser when I send an Intent with ACTION_SEND and MIME type "image/png". The problem is that some phones come with a default messaging app that is not showing up in the list, mine included (myTouch 4G) as well as a user that I speak with over email (using an HTC EVO). If I choose a Send or Share option from the built in gallery app or another application on the same image I'm saving and attempting to send directly from my app, Messages shows up in the list. From my app it does not. Other phones don't have this problem, so it's clearly a platform specific thing. But that doesn't mean I should just ignore the problem.
So, I go to troubleshooting the issue. I register one of the activities in my app to receive the the same type of intent, and then hit a breakpoint to analyze the Intent object being sent from the two different ways of sending it.
The problem is, the intent I'm sending and the intent being sent from Gallery or AndroZip (where Messages does show up in the chooser) seem to be the same. They both have the same action, same categories, same flags, same mime type. What else can I inspect on the Intent from Gallery or AndroZip to tell if there's some more information I can add to my Intent to get the default messaging app to show up in the chooser in cases where it is not?
The problem is specific to HTC Sense phones, and it arises because their Gallery and Messaging apps are different to the stock ones.
Specifically the Intent sent from Gallery to Messaging has the action android.intent.action.SEND_MSG which is different to android.intent.action.SEND. The Sense messaging app doesn't handle SEND, unlike the stock messaging app.
So the question becomes, how is the Sense Gallery app creating an activity chooser dialog which combines both SEND and SEND_MSG ?
I've done some research and got mostway there... the code below works, but the "Messages" entry in the dialog appears at the top rather than in alphabetical order as per Gallery. Doubtless some more research into intents would correct that, but at least this works:
// Create a chooser for things that can ACTION_SEND images
Intent intent = new Intent(Intent.ACTION_SEND);
Uri data = Uri.parse("content://media/external/images/media/98");
intent.putExtra(Intent.EXTRA_STREAM, data);
intent.setType("image/jpeg");
Intent chooser = Intent.createChooser(intent, "Blah");
// Add the stupid HTC-Sense-specific secondary intent
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.putExtra(Intent.EXTRA_STREAM, data);
htcIntent.setType("image/jpeg");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
// Show the chooser
startActivity(chooser);
First of all, +1 to Reuben, he is the genius, not me. But I had to modify his code a bit to get it to work. Basically I had to putExtra() on the htcIntent or the image never got stuck to the Intent.
Tested and validated on a Droid X and HTC Incredible (which had the same problem until now thanks to Reuben).
Uri uri = Uri.fromFile(new File(mFile));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.setType("image/png");
htcIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, "Send Method");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
startActivity(chooser);
Edit: I realize I'm putting the image on two Intents now, but I couldn't get it to work any other way.
Instead of debugging the intents, why not try to compare how your starting the chooser with how the gallery is doing it. It is open source after all, so instead of trying to guess at the issue with the result, you can debug from the cause.
https://android.googlesource.com/platform/packages/apps/Gallery3D

Categories

Resources