【问题标题】:Unable to decode stream: FileNotFoundException无法解码流:FileNotFoundException
【发布时间】:2013-05-07 08:31:35
【问题描述】:

在我的数据库中,我为手机上的图片的每个条目提供一个 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

【问题讨论】:

  • 不需要文件扩展名吗?比如10139.jpg 什么的?
  • 我试过了,但不,我不需要它
  • 那么我认为它与图像路径有关。它明确表示没有这样的文件。你的图片在哪里?
  • 图片显示在图库中。
  • 好的,但是这张图片在你记忆中的什么位置,在你的 SD 卡上还是在其他地方?

标签: android image bitmap camera


【解决方案1】:

你没发现问题吗?你有很多解决方案。

  1. ebild 更改为/DCIM/Camera/filename.jpg 格式的值,然后像这样使用imgeUri:

    imageUri = Environment.getExternalStorageDirectory().getAbsolutePath() + uri.toString();
    
  2. ebild 更改为filename.jpg 格式的值,然后像这样使用imgeUri:

    imageUri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/" + uri.toString();
    
  3. 将整个图片路径分配给ebild like:

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

【讨论】:

  • 不,肯定有不同的问题。我添加了一个硬编码的文件路径,它也没有显示出来!
  • 你添加了什么硬编码路径?你在哪里添加的?试试这个:BitmapDrawable b = new BitmapDrawable(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/" + "filename.jpg"); 其中 filename.jpg 是一些现有图像的名称
  • 我添加了这个:/storage/emulated/0/DCIM/Camera/1368438768752.jpg
  • 当您创建捕获图像的意图时,imageUri 是使用相机拍摄图像后将保存的文件位置。如果您想使用第三种解决方案,您应该将imageUri 作为ebild 插入到数据库中。 ebild = imageUri。对于文件名,您可以使用某种时间戳。以时间戳形式查看文件名的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2017-07-23
相关资源
最近更新 更多