我正在开发一个简单的应用程序,它从相机捕获图像并将其保存到存储中。我无法弄清楚这里出了什么问题......当用户拍照并接受它时,应用程序崩溃了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
File f = createImageFile();
photoFile = Uri.fromFile(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
photoFile);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
Button saveButton = (Button) this.findViewById(R.id.button3);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
save2Samba("hello", "kfir.txt");
//Your code goes here
}
catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
日志猫 说
12-29 18:01:20.893: E/AndroidRuntime(8026): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.hofyam/com.example.hofyam.MainActivity}: java.lang.NullPointerException
请帮忙..我被卡住了。谢谢