Reputation: 397
I'm developing an android app for data collection. This app should have the possibility to take photos, videos, audio-content by using the usual intent-mechanisms.
To start the image-capture intent is no problem. It is shown, and I can take a photo, which is saved on the sdcard. But in onActivityResult()
the expected values aren't returned, if "Video" or "Photo" are choosed. resultCode
is != RESULT_OK
, and data.getData()
returns null
. Getting "Audio" seems to be no problem...
There are many tutorials and howtos on the web, but I didn't find that difference, which may result in the behavior of my app...
What is wrong?
For debugging, I'm using a Motorola Defy with Android 2.2, which is connected via USB. The USB-mode is set no "none", so my computer doesn't lock the sd-card.
Edit 1:
As a first try, I added these lines into AndroidManifest.xml
, but there's no difference:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Edit 2:
Adding super.onActivityResult(requestCode, resultCode, data);
to onActivityResult()
in the activity made no difference
Edit 3:
Tried to change the line
toDo.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
to
toDo.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/image.jpg")));
but this doesn't work either. The image is saved at the given uri-position, but the app crashes, when it returns to my activity.
Here's the stack (translated from german locale to english):
Thread [<1> main] (Paused (Exception RuntimeException))
ActivityThread.deliverResults(ActivityThread$ActivityRecord, List) Line: 3605
ActivityThread.handleSendResult(ActivityThread$ResultData) Line: 3647
ActivityThread.access$3000(ActivityThread, ActivityThread$ResultData) Line: 129
ActivityThread$ResultData(ActivityThread$H).handleMessage(Message) Line: 2147
ActivityThread$H(Handler).dispatchMessage(Message) Line: 99
Looper.loop() Line: 143
ActivityThread.main(String[]) Line: 4717
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) Line: not available [native method]
Method.invoke(Object, Object...) Line: 521
ZygoteInit$MethodAndArgsCaller.run() Line: 860
ZygoteInit.main(String[]) Line: 618
NativeStart.main(String[]) Line: not available [native method]
Edit 4:
Reverted Edit 3.
Had a look on the return-values of onActivityResult
again. In the case of video or image/photo this is both right: requestCode==0x100
or resultCode==0x101
, but resultCode==0
and data==null
.
Code:
This is my code:
package test.mediastore;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class StartActivity extends Activity {
private Button ImageCaptureButton = null;
private Button VideoCaptureButton = null;
private Button AudioCaptureButton = null;
private TextView InfoTextView = null;
final static int IMAGE_CAPTURE = 0x100;
final static int VIDEO_CAPTURE = 0x101;
final static int AUDIO_CAPTURE = 0x102;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageCaptureButton = (Button)findViewById(R.id.ImageCaptureButton);
ImageCaptureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent toDo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
toDo.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(toDo, IMAGE_CAPTURE);
}
});
VideoCaptureButton = (Button)findViewById(R.id.VideoCaptureButton);
VideoCaptureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent toDo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
toDo.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(toDo, VIDEO_CAPTURE);
}
});
AudioCaptureButton = (Button)findViewById(R.id.AudioCaptureButton);
AudioCaptureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent toDo = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
toDo.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(toDo, AUDIO_CAPTURE);
}
});
InfoTextView = (TextView)findViewById(R.id.InfoTextView);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); // Edit 2 - made no difference
if (resultCode == RESULT_OK) {
switch (requestCode) {
case IMAGE_CAPTURE:
InfoTextView.setText("Image");
break;
case VIDEO_CAPTURE:
InfoTextView.setText("Video");
break;
case AUDIO_CAPTURE:
InfoTextView.setText("Audio");
break;
default:
InfoTextView.setText("None");
}
} else {
InfoTextView.setText("resultCode != RESULT_OK");
}
}
}
Upvotes: 2
Views: 3376
Reputation: 12678
From your edit3, you need to do this line;
toDo.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/image.jpg")));
But you actually need to save that File object into a instance variable, then on activity result, if the result is ok, then you just use the File output to access the image/video and so on. If you want, you can insert this into the mediaStore manually on activity result (easily found by googling)
Upvotes: 1