【发布时间】:2013-12-06 04:33:21
【问题描述】:
我的应用程序中有一个自定义相机,我用它来捕捉照片,并将其保存在 SD 卡中的文件夹中。我能够捕获图像,但我需要在另一个活动中获取图像。我在 onActivityResult 事件中收到失败交付结果和 NullPointer 异常。我知道图像正在保存,因为我可以看到图像的路径,但图像本身会在一段时间后显示在文件夹中。我不确定这是否是 NullPointerException 的原因。我一直在尝试 StackOverflow 的各种解决方案,但没有解决问题。请有人尽快帮助我。
这是我的代码的 sn-ps: CustomCameraActivity.java 文件:
public class CustomCameraActivity extends Activity {
private static final String TAG = "CameraActivity";
CameraPreview camPreview;
Button btn_take_pic, btn_use_pic;
Camera mCamera;
String fileName;
Activity act;
Context ctx;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
act = this;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_custom_camera);
camPreview = new CameraPreview(this,
(SurfaceView) findViewById(R.id.surfaceView));
camPreview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
((FrameLayout) findViewById(R.id.preview)).addView(camPreview);
camPreview.setKeepScreenOn(true);
addListenerForButton();
}
private void addListenerForButton() {
btn_take_pic = (Button) findViewById(R.id.btn_take_pic);
btn_take_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "Before taking picture");
mCamera.stopPreview();
mCamera.startPreview();
mCamera.takePicture(null, null, jpegCallback);
Log.e(TAG, "After taking picture");
btn_take_pic.setText("Retake");
// mCamera.startPreview();
}
});
btn_use_pic = (Button) findViewById(R.id.btn_usePhoto);
btn_use_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = getOutputMediaFile();
Log.e("use_pic buttn ", file.getPath());
Intent intent = new Intent();
intent.putExtra("output", file.getPath());
finish();
}
});
}
...
PictureCallback jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.e(TAG, "Reached jpegcallback");
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.e("blah3", "picture file was null!");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.e(TAG, "picture file not found!");
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
Log.e(TAG, "hooray " + pictureFile.toURI().getRawPath());
}
};
private static File getOutputMediaFile() {
Log.e(TAG, "Get me the o/p file");
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"PostcareApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e("PostcareApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
Log.e(TAG, "Before returning " + mediaFile.getAbsolutePath());
return mediaFile;
}
需要图片的Activity:
public class HowItWorksActivity extends Activity {
ImageButton btn_Account, btn_Photo, btn_Edit, btn_Flip, btn_Post;
RelativeLayout rl;
private static final int CAMERA_PIC_REQUEST = 2500;
private static final int SELECT_PICTURE = 1;
Bitmap postcard_image;
Uri uri_image;
...
private void takepicture() {
Intent cameraIntent = new Intent(HowItWorksActivity.this,CustomCameraActivity.class);
startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("Howitworks..", "in activityresult");
String imgfile = (String) getIntent().getExtras().get("output");
Log.e("image ", imgfile);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
postcard_image = BitmapFactory.decodeFile(imgfile);
postcard_image = Bitmap.createScaledBitmap(postcard_image,
metrics.widthPixels, 600, true);
imgview.setImageBitmap(postcard_image);
}
}
这里是 LogCat 文件:
12-06 12:27:40.779: E/CameraActivity(16607): Get me the o/p file
12-06 12:27:40.779: E/CameraActivity(16607): Before returning /mnt/sdcard/Pictures/PostcareApp/IMG_20131206_122740.jpg
12-06 12:27:40.779: E/use_pic buttn(16607): /mnt/sdcard/Pictures/PostcareApp/IMG_20131206_122740.jpg
12-06 12:27:41.309: E/Howitworks..(16607): in activityresult
12-06 12:27:41.499: E/AndroidRuntime(16607): FATAL EXCEPTION: main
12-06 12:27:41.499: E/AndroidRuntime(16607): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2500, result=0, data=null} to activity {com.sample.postcare2/com.sample.postcare2.HowItWorksActivity}: java.lang.NullPointerException
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3437)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.ActivityThread.access$1100(ActivityThread.java:139)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.os.Looper.loop(Looper.java:154)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.ActivityThread.main(ActivityThread.java:4944)
12-06 12:27:41.499: E/AndroidRuntime(16607): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 12:27:41.499: E/AndroidRuntime(16607): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 12:27:41.499: E/AndroidRuntime(16607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 12:27:41.499: E/AndroidRuntime(16607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 12:27:41.499: E/AndroidRuntime(16607): at dalvik.system.NativeStart.main(Native Method)
12-06 12:27:41.499: E/AndroidRuntime(16607): Caused by: java.lang.NullPointerException
12-06 12:27:41.499: E/AndroidRuntime(16607): at com.sample.postcare2.HowItWorksActivity.onActivityResult(HowItWorksActivity.java:217)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.Activity.dispatchActivityResult(Activity.java:4740)
12-06 12:27:41.499: E/AndroidRuntime(16607): at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
12-06 12:27:41.499: E/AndroidRuntime(16607): ... 11 more
【问题讨论】:
标签: android camera nullpointerexception android-sdcard