我正在使用相机或相册为我的应用拍照。但有时在点击相机中的图像然后切换到图库,图库崩溃。
代码如下:
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));
}
}
什么让画廊崩溃。请帮助我。
答案 0 :(得分:3)
所以,经过多次杂耍,我发现这个解决方案对我有用..
打开图库或相机: -
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) {
int apiLevel = Integer.parseInt(Build.VERSION.SDK);
if(apiLevel < 11) {
try{
//String fileName = "temp.jpg";
Calendar c = Calendar.getInstance();
CAPTURE_TITLE = "si_"+c.getTimeInMillis()+".jpg";
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE);
Uri ImageURI = Uri.fromFile(file);
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
ImageURI );
startActivityForResult(intent, 99);
}
catch(Exception e)
{
LogCreator lg = new LogCreator();
lg.writeBarcodeToFile("Image exc1", e.toString());
}
}
else{
//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();
现在是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 == 99 && resultCode != 0) {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE);
fileName = Environment.getExternalStorageDirectory() + "/DCIM/Camera/"+CAPTURE_TITLE;
Uri imgUri = Uri.fromFile(file);
//new GetThumbnail().execute(imgUri);
mUploadImage.setText(CAPTURE_TITLE);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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));
}
}
这些是我用来获取路径的方法: -
/*
* Returns image real path.
*/
private String getRealPathFromURI(final Uri contentUri) {
final String[] proj = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
final Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/*
* Cuts selected file name from real path to show in screen.
*/
private String getStringNameFromRealPath(final String bucketName) {
return bucketName.lastIndexOf('/') > 0 ? bucketName
.substring(bucketName.lastIndexOf('/') + 1) : bucketName;
}
希望得到这个帮助。