OnClickListener oclBtnOk = new OnClickListener() {
@Override
public void onClick(View v) {
String fileName = "Camera_Example.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
/*********** Load Captured Image And Data Start ****************/
String imageId = convertImageUriToFile(imageUri, CameraActivity);
// Create and excecute AsyncTask to load capture image
new LoadImagesFromSDCard().execute("" + imageId);
/*********** Load Captured Image And Data End ****************/
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, " Picture was not taken ",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " Picture was not taken ",
Toast.LENGTH_SHORT).show();
}
}
}
public class LoadImagesFromSDCard extends AsyncTask<String, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
Bitmap mBitmap;
protected void onPreExecute() {
/****** NOTE: You can call UI Element here. *****/
// Progress Dialog
Dialog.setMessage(" Loading image from Sdcard..");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null;
try {
uri = Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""
+ urls[0]);
/************** Decode an input stream into a bitmap. *********/
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
if (bitmap != null) {
/********* Creates a new bitmap, scaled from an existing bitmap. ***********/
newBitmap = Bitmap.createScaledBitmap(bitmap, 350, 350,
true);
// SaveIamge(newBitmap);
bitmap.recycle();
if (newBitmap != null) {
mBitmap = newBitmap;
}
}
} catch (IOException e) {
// Error fetching image, try to recover
/********* Cancel execution of this task. **********/
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
if (mBitmap != null) {
showImg.setImageBitmap(mBitmap);
}
}
}
}
我已经编写了上面的代码来捕获图像并显示为缩略图。它在android tab 2和huwawi tab中完美运行。但是当我在TAb 3上尝试它时会出现以下错误,你能否帮我避免这个错误。
11-18 10:16:04.882: E/AndroidRuntime(7289): java.lang.RuntimeException: Unable to resume activity {com.example.cap_im/com.example.cap_im.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.cap_im/com.example.cap_im.MainActivity}: java.lang.NullPointerException
11-18 10:16:04.882: E/AndroidRuntime(7289): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2658)