此应用程序用于测试在Android中使用内置摄像头??? 我的代码中导致此错误的错误是什么
我正在启动相机并拍摄照片,然后我想在imageView中显示它
我发现照片存储在SD卡中但不能在onActivityResult方法中使用
package com.world.hello;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
"/sdcard/Pictures/MyCameraApp/IMAG.jpg")));
startActivityForResult(intent, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 100) {
Log.d("Image", data.getData().toString());
// ImageView iv = (ImageView) findViewById(R.id.imageView1);
// iv.setImageBitmap(BitmapFactory.decodeFile(data.getData().toString()));
}
}
}
}
为什么会出现这个错误?
05-24 17:19:40.164: E/AndroidRuntime(4759): FATAL EXCEPTION: main
05-24 17:19:40.164: E/AndroidRuntime(4759): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.world.hello/com.world.hello.Main}: java.lang.NullPointerException
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.ActivityThread.access$1100(ActivityThread.java:123)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.os.Looper.loop(Looper.java:137)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 17:19:40.164: E/AndroidRuntime(4759): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 17:19:40.164: E/AndroidRuntime(4759): at java.lang.reflect.Method.invoke(Method.java:511)
05-24 17:19:40.164: E/AndroidRuntime(4759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 17:19:40.164: E/AndroidRuntime(4759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 17:19:40.164: E/AndroidRuntime(4759): at dalvik.system.NativeStart.main(Native Method)
05-24 17:19:40.164: E/AndroidRuntime(4759): Caused by: java.lang.NullPointerException
05-24 17:19:40.164: E/AndroidRuntime(4759): at com.world.hello.Main.onActivityResult(Main.java:40)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
05-24 17:19:40.164: E/AndroidRuntime(4759): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
05-24 17:19:40.164: E/AndroidRuntime(4759): ... 11 more
答案 0 :(得分:2)
Log.d("Image", data.getData().toString());
您无需获取数据。您已经知道图像保存到的文件的位置。直接使用它。