我尝试了这段代码:
public class pantalla8 extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
public static final int MEDIA_TYPE_IMAGE = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
final Button camera = (Button) findViewById(R.id.boton12);
camera.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
} else {
return null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelado", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Falla", Toast.LENGTH_SHORT).show();
}
}
}
}
正确创建了MyCameraApp文件并将照片正确存储在此目录中,但我收到了运行时异常:
10-04 14:22:16.398: E/AndroidRuntime(6952): FATAL EXCEPTION: main
10-04 14:22:16.398: E/AndroidRuntime(6952): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {tian.proto/tian.proto.pantalla8}: java.lang.NullPointerException
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.os.Looper.loop(Looper.java:130)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-04 14:22:16.398: E/AndroidRuntime(6952): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 14:22:16.398: E/AndroidRuntime(6952): at java.lang.reflect.Method.invoke(Method.java:507)
10-04 14:22:16.398: E/AndroidRuntime(6952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-04 14:22:16.398: E/AndroidRuntime(6952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-04 14:22:16.398: E/AndroidRuntime(6952): at dalvik.system.NativeStart.main(Native Method)
10-04 14:22:16.398: E/AndroidRuntime(6952): Caused by: java.lang.NullPointerException
10-04 14:22:16.398: E/AndroidRuntime(6952): at tian.proto.pantalla8.onActivityResult(pantalla8.java:95)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
10-04 14:22:16.398: E/AndroidRuntime(6952): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
10-04 14:22:16.398: E/AndroidRuntime(6952): ... 11 more
我不知道为什么......谁能告诉我我做错了什么?我应该尝试在AsyncTask线程中调用相机意图吗?
提前致谢!
答案 0 :(得分:0)
使用断点和调试以确保,但您在onActivityResult
中进行的唯一对象调用是data.getData()
,所以我猜测数据为空。