在我的数据库中,每个条目都有一个uri到手机上的图片。要在手机上显示它,Listview有一个CustomAdapter。现在,我想在ListView中显示图片并收到以下错误消息:
05-13 12:20:34.234: E/BitmapFactory(17684): Unable to decode stream: java.io.FileNotFoundException: /external/images/media/10139: open failed: ENOENT (No such file or directory)
BitmapDrawable: cannot decode external/images/media/10139
它发生在这个功能:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEreignis holder;
if(convertView == null){
convertView = inflator.inflate(R.layout.list_ereignis, parent, false);
holder = new ViewHolderEreignis((TextView) convertView.findViewById(R.id.enullline), (TextView) convertView.findViewById(R.id.efirstLine), (ImageView) convertView.findViewById(R.id.eimgv));
convertView.setTag(holder);
}
else{
holder = (ViewHolderEreignis) convertView.getTag();
}
Ereignis ki = (Ereignis) getItem(position);
holder.getEreignisname().setText(ki.getEreignisname());
holder.getEreignisdatum().setText(ki.getEreignisZeit());
Uri uri = Uri.parse(ki.getEreignisbild());
BitmapDrawable b = new BitmapDrawable(ki.getEreignisbild());
System.out.println("ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ" + uri.getPath());
holder.getEreignisbild().setImageDrawable(b);
return convertView;
}
如您所见,我输出的图像的URI如下所示:
external/images/media/10139
有人知道这个错误吗?
修改 这是我添加图像的代码:
ContentValues values = new ContentValues();
String TITLE = null;
values.put(MediaStore.Images.Media.TITLE, TITLE);
String DESCRIPTION = null;
values.put(MediaStore.Images.Media.DESCRIPTION, DESCRIPTION);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
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);
System.out.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVvv" + imageUri.getPath());
startActivityForResult(intent, IMAGE_CAPTURE);
EDIT2:
以下是我将链接添加到数据库的代码:
public void insertereignis(String ename, String ezeit, String egenaueres, String ebild, int kindid){
long rowId = -1;
try{
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
System.out.println(ename + ezeit + egenaueres);
cv.put(EREIGNISNAME, ename);
cv.put(EREIGNISZEIT, ezeit);
cv.put(EREIGNISGENAUERES, egenaueres);
cv.put(EREIGNISBILD, ebild);
System.out.println("GRIAISDALSDJLASJDLKASJDKLAJSDLKJFS" + ebild);
cv.put(KINDID, kindid);
rowId = db.insert(TABLE_NAME_EREIGNIS, null, cv);
}
catch (SQLiteException e){
Log.e(TAG, "insert() Ereignis", e);
}
finally{
Log.d(TAG, "insert(): Ereignis rowId=" + rowId);
}
}
ebild的值为/external/images/media/10146
答案 0 :(得分:0)
将ebild
更改为格式/DCIM/Camera/filename.jpg
的值,然后像这样使用imgeUri:
imageUri = Environment.getExternalStorageDirectory().getAbsolutePath() + uri.toString();
将ebild
更改为格式filename.jpg
的值,然后像这样使用imgeUri:
imageUri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/" + uri.toString();
将整个图片路径指定为ebild
,如:
ebild = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/" + "filename.jpg"
然后将其用作图像uri而不做任何修改。
我不知道还能说些什么。解决方案很明显。
修改强>
以下是如何将文件名设置为时间戳:
Calendar c = Calendar.getInstance();
String cameraImageFileName = "IMG_" + c.get(Calendar.YEAR) + "-" +
((c.get(Calendar.MONTH)+1 < 10) ? ("0" + (c.get(Calendar.MONTH)+1)) : (c.get(Calendar.MONTH)+1)) + "-" +
((c.get(Calendar.DAY_OF_MONTH) < 10) ? ("0" + c.get(Calendar.DAY_OF_MONTH)) : c.get(Calendar.DAY_OF_MONTH)) + "_" +
((c.get(Calendar.HOUR_OF_DAY) < 10) ? ("0" + c.get(Calendar.HOUR_OF_DAY)) : c.get(Calendar.HOUR_OF_DAY)) + "-" +
((c.get(Calendar.MINUTE) < 10) ? ("0" + c.get(Calendar.MINUTE)) : c.get(Calendar.MINUTE)) + "-" +
((c.get(Calendar.SECOND) < 10) ? ("0" + c.get(Calendar.SECOND)) : c.get(Calendar.SECOND)) + ".jpg";
您将获得IMG_YYYY-MM-DD_HH-MM-SS.jpg
,您的imageUri
将是:
imageUri = Environment.getExternalStorageDirectory().getAbsolutePath() + cameraImageFileName;
您应该在数据库中将其添加为ebild
:
insertereignis(ename,ezeit,egenaueres, imageUri ,kindid);