[英]Android take photo - java.lang.NullPointerException
我要在“活動”中拍照,但出現此錯誤:
活動加載正常,當我按一下拍照時便崩潰了。
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 nnnnnnnnnnnn nnnnnnnnnnnn nnnnnnnnnnnnnnnnnn這是我的代碼的公園:
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的:
來自fragemnt:
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
使用此代碼:
@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();
}
我使用以下代碼在我的應用上拍照,效果很好,請嘗試以下操作:
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
}
}
希望這對您有所幫助。
嘗試檢查
if (data != null)
因為可能有多種原因導致數據為空 。
更新:
intent.putExtra("outFile", fileUri);
至
intent.putExtra("outFile", fileUri.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.