我正在使用相机或图库为我的应用程序拍照。但有时在单击相机中的图像然后切换到图库后,图库会崩溃。
代码如下:
case R.id.etUploadImage:
Log.d(TAG, " add photo");
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(getApplicationContext(), "sdcard not mounted",
Toast.LENGTH_SHORT).show();
break;
}
AlertDialog.Builder photoDialog = new AlertDialog.Builder(this);
photoDialog
.setTitle("Photo source")
.setCancelable(true)
.setPositiveButton("Open Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
54);
}
})
.setNegativeButton("Open Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
//String fileName = "temp.jpg";
Date date = new Date();
DateFormat df = new SimpleDateFormat("-mm-ss");
String newPicFile = "Bild"+ df.format(date) + ".jpg";
String outPath = "/sdcard/" + newPicFile;
File outFile = new File(outPath);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,
outFile.getAbsolutePath());
mCapturedImageURI = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(intent, 96);
}
});
photoDialog.show();
break;
}
并且 onActivityResult() 是:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 54 && resultCode == RESULT_OK) {
if(data !=null){
fileName = getRealPathFromURI(data.getData());
mUploadImage.setText(getStringNameFromRealPath((String) fileName));
} else {
Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == 96 && resultCode != 0) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
fileName = cursor.getString(column_index_data);
String s[] = fileName.split("/");
mUploadImage.setText(s[s.length - 1]);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI));
}
}
是什么让画廊崩溃。请帮助我。