我正在拍摄我的活动照片,我收到了这个错误:
活动加载正常,当我按下拍照然后崩溃。
12-24 17:22:19.347: E/AndroidRuntime(32764): FATAL EXCEPTION: main
12-24 17:22:19.347: E/AndroidRuntime(32764): java.lang.NullPointerException
12-24 17:22:19.347: E/AndroidRuntime(32764): at java.io.File.fixSlashes(File.java:185)
12-24 17:22:19.347: E/AndroidRuntime(32764): at java.io.File.<init>(File.java:134)
12-24 17:22:19.347: E/AndroidRuntime(32764): at com.example.CameraActivity$1.onPictureTaken(CameraActivity.java:287)
12-24 17:22:19.347: E/AndroidRuntime(32764): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:855)
12-24 17:22:19.347: E/AndroidRuntime(32764): at android.os.Handler.dispatchMessage(Handler.java:107)
12-24 17:22:19.347: E/AndroidRuntime(32764): at android.os.Looper.loop(Looper.java:194)
12-24 17:22:19.347: E/AndroidRuntime(32764): at android.app.ActivityThread.main(ActivityThread.java:5449)
12-24 17:22:19.347: E/AndroidRuntime(32764): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 17:22:19.347: E/AndroidRuntime(32764): at java.lang.reflect.Method.invoke(Method.java:525)
12-24 17:22:19.347: E/AndroidRuntime(32764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-24 17:22:19.347: E/AndroidRuntime(32764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-24 17:22:19.347: E/AndroidRuntime(32764): at dalvik.system.NativeStart.main(Native Method)
nnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn 这是我的代码公园:
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.e("camera test", "1111");
File pictureFile = new File( outFile ); //********* LINE 287
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: ");
return;
}
/*
imageCrop(data, pictureFile, 0, (int)(1200 / 720 * top_px), 1200, 1200);
Intent returnIntent = new Intent();
returnIntent.putExtra("capturedFile",pictureFile.getAbsolutePath());
setResult(RESULT_OK,returnIntent);
finish();
*/
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap btemp = BitmapFactory.decodeFile(outFile,options);
Log.e("picture captured", options.outHeight + "|" + options.outWidth);
Intent returnIntent = new Intent();
returnIntent.putExtra("capturedFile",pictureFile.getAbsolutePath());
setResult(RESULT_OK,returnIntent);
finish();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
-----------------------更新代码---------------------- ---------------------------------------------
我有一个调用自定义拍照活动的片段。
额外传递给活动调用的意图不起作用。如果我打印logcat,我可以看到片段中的意图。但活动没有收到它。
片段:
Intent intent = new Intent();
intent.setClass(getActivity(), CameraActivity.class);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra("outFile", fileUri);
Log.e(TAG, "" +
"------------------------------------------------------ " +
"fileUri intent => " + fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Intent intent = getIntent();
outFile = intent.getStringExtra("outFile");
String test = getIntent().getExtras().getString("outFile");
Log.i("CameraActivity", "" +
"- " +
"test intent => " + test);
Log.e("CameraActivity", "" +
"- " +
"intent.getStringExtra(outFile) => " + intent.getStringExtra("outFile"));
Log.e("CameraActivity", "" +
"- " +
"outFile intent => " + outFile);
logcat的:
12-26 11:12:21.399: E/PostProductFragment(3563): - fileUri intent => file:///mnt/sdcard/Pictures/MyCameraApp/IMG_20141226_111221.jpg
来自活动日志:
12-26 11:12:21.449: I/CameraActivity(3563): - test intent => null
12-26 11:12:21.449: E/CameraActivity(3563): - intent.getStringExtra(outFile) => null
12-26 11:12:21.449: E/CameraActivity(3563): - outFile intent => null
答案 0 :(得分:1)
使用此代码:
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
fileUri1 = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri1);
}
});
返回后调用此方法:
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.host_img_showlogo);
imageView.setImageBitmap(photo);
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri1.getPath(), options);
Image_path= fileUri1.getPath();
}
答案 1 :(得分:0)
我使用此代码在我的应用上拍照,这很好用,试试这个:
Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(c, FROM_CAMERA);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == FROM_CAMERA && resultCode == RESULT_OK && null != data )
{
Bundle extras = data.getExtras();
Bitmap photo = extras.getParcelable("data");
//Post here your code to get the path of the photo
}
}
希望这会对你有所帮助。
答案 2 :(得分:0)
尝试检查
if (data != null)
因为数据可能有很多原因 null 。
更新:
intent.putExtra("outFile", fileUri);
到
intent.putExtra("outFile", fileUri.toString());