I had a class that access the native camera to take picture. After the picture had been taken, it will be save in a folder and that picture will be display in a new activity.
The problem is, i try to the get the data i put into the intent after the picture had been taken but it always says that the intent is null.pointer.exception
. Below is my class, anyone please help me.
import java.io.File;
import android.app开发者_开发百科.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class CameraTestActivity extends Activity {
Button start;
final int TAKE_PICTURE = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.startButton);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File file = new File(Environment.getExternalStorageDirectory().toString() + "/testImage/" + "toBeUpload.jpg");
Uri imageUri = Uri.fromFile(file);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntent.putExtra("path", imageUri.toString());
startActivityForResult(cameraIntent,TAKE_PICTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode == RESULT_OK)
{
if(requestCode == TAKE_PICTURE)
{
/*ERRROR OCCUR HERE*/
Bundle extras = intent.getExtras();
}
}
}
}
Rich and userSeven7s are right, but it's rather besides the point in your particular case: you don't actually need the Intent's return data for the photo chooser (exception: some buggy very old phones only write a thumbnail in the extras and don't actually write an image to the path you specified, but that's another story) -- just look for the image written to the URI you passed the intent originally.
I wonder if the call to super.onActivityResult is consuming the Intent instance and setting it to null when it's through with it. You don't need that call to the super class since you're implementing it yourself, so give that a try.
Call the super.onActivityResult(requestCode, resultCode, intent);
after you process the event.
Try:
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Referencing: http://achorniy.wordpress.com/2010/04/26/howto-launch-android-camera-using-intents/
精彩评论