我想实现的是,在按钮点击时打开相机,用户拍照后返回应用程序,并在图像视图中显示拍摄图片的缩略图。
package com.example.waterbill;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Bill_meter_satus extends Activity {
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
Button b1, b2;
EditText edt1;
ImageView mImage;
private static final int CAMERA_PIC_REQUEST = 1888;
String values;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bill_meter_sat);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
edt1 = (EditText) findViewById(R.id.editText1);
Bundle extra = getIntent().getExtras();
if (extra != null) {
values = extra.getString("Cons_Code");
edt1.setText(values);
Toast.makeText(Bill_meter_satus.this, values, Toast.LENGTH_SHORT)
.show();
}
//点击按钮拍摄快照
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
b1.setOnClickListener(new View.OnClickListener() {
//on click of button move to next activity
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent next = new Intent(
"com.example.waterbill.Bill_meter_status_normal");
next.putExtra("Cons_Code", values);
startActivity(next);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == CAMERA_PIC_REQUEST) {
// 2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImage.setImageBitmap(thumbnail);
// 3
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// 4
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
// 5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
日志记录器
09-04 07:26:47.051: E/AndroidRuntime(12992): FATAL EXCEPTION: main
09-04 07:26:47.051: E/AndroidRuntime(12992): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/18169 typ=image/jpeg (has extras) }} to activity {com.example.waterbill/com.example.waterbill.Bill_meter_satus}: java.lang.NullPointerException
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.ActivityThread.deliverResults(ActivityThread.java:3007)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3050)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.os.Looper.loop(Looper.java:137)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.ActivityThread.main(ActivityThread.java:4448)
09-04 07:26:47.051: E/AndroidRuntime(12992): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 07:26:47.051: E/AndroidRuntime(12992): at java.lang.reflect.Method.invoke(Method.java:511)
09-04 07:26:47.051: E/AndroidRuntime(12992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-04 07:26:47.051: E/AndroidRuntime(12992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-04 07:26:47.051: E/AndroidRuntime(12992): at dalvik.system.NativeStart.main(Native Method)
09-04 07:26:47.051: E/AndroidRuntime(12992): Caused by: java.lang.NullPointerException
09-04 07:26:47.051: E/AndroidRuntime(12992): at com.example.waterbill.Bill_meter_satus.onActivityResult(Bill_meter_satus.java:88)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
09-04 07:26:47.051: E/AndroidRuntime(12992): at android.app.ActivityThread.deliverResults(ActivityThread.java:3003)
09-04 07:26:47.051: E/AndroidRuntime(12992): ... 11 more