gpt4 book ai didi

android - 图库(进程 com.cooliris.media)意外停止

转载 作者:搜寻专家 更新时间:2023-11-01 09:02:29 26 4
gpt4 key购买 nike

我正在使用相机或图库为我的应用拍照。但有时在单击相机中的图像然后切换到图库后,图库会崩溃。

代码如下:

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));
}
}

是什么导致画廊崩溃。请帮助我。

最佳答案

所以,经过多次尝试后,我找到了适合我的解决方案..

要打开相册或相机:-

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;
}

希望对您有所帮助。

关于android - 图库(进程 com.cooliris.media)意外停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14038275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com