android获取缩略图是FileNotFound

阔活洵信 2014-07-16 09:31:57
情况大概是这样的。。.我用ListView显示一个文件夹里面的照片或者视频,包括描该文件的标题和日期什么之类的。。。但是在现实缩略图时,却出现下面的错误
Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@4298ee50: open failed: ENOENT (No such file or directory)
当然上面说找不到文件的地址是一个内存,这个内存里存的是生成的缩略图,加载数据的代码如下:

/**
* 加载数据
*/
private void AddData()
{
List<Map<String, Object>> list = getData();
SimpleAdapter listItemAdapter = new SimpleAdapter(this, list, R.layout.listview,
new String[]{"pic","title","info"},
new int[]{R.id.list_pic,R.id.list_title,R.id.list_info});
listView.setAdapter(listItemAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Intent intent = new Intent(MediaListActivity.this,MediaActivity.class);
intent.putExtra("path", mediaFiles.get(arg2).getPath());
startActivity(intent);
}
});
}

其中getData()代码如下:

/**
* 获取数据
* @return
*/
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
mediaFiles = MediaFile.getInfo();
for (int i = 0; i < mediaFiles.size(); i++) {
MediaFile mediafile = mediaFiles.get(i);
String filePath = mediafile.getPath();
Map<String, Object> map = new HashMap<String, Object>();
if (!new File(filePath).exists()) {
continue;
}
Bitmap mediaThumb;
if(filePath.endsWith("mp4"))
{
mediaThumb = getVideoThumbnail(filePath, 72, 72, MediaStore.Images.Thumbnails.MICRO_KIND);
}
else {
mediaThumb = getImageThumbnail(filePath, 72, 72);
}
map.put("pic", mediaThumb);
map.put("title", mediafile.getTitle());
map.put("info", mediafile.getShotTime());
list.add(map);
}
return list;
}

报错的过程也很微妙,就是我单步调试不会出错,等我调试完了之后,再开屏幕去看的时候,logcat里面才会出现这个错误,而缩略图也显示不出来
...全文
628 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰冷的城市 2015-11-11
  • 打赏
  • 举报
回复
楼主你是怎么解决的呀 求代码啊
jijiancheng 2014-12-15
  • 打赏
  • 举报
回复
十分感谢,按照你的做法解决了我的问题!
w123456776 2014-08-19
  • 打赏
  • 举报
回复
重写这个类你怎么做的啊 我也在做这个 求贴下代码
sagittarius1988 2014-07-16
  • 打赏
  • 举报
回复
引用 6 楼 zxhm001 的回复:
[quote=引用 1 楼 sagittarius1988 的回复:] 不是提示文件找不到么,看看路径
解决方法找到,结贴了,多谢这位大哥的热心帮助[/quote] 对于Adapter ,基本上都是重载,里面的方法自己实现
阔活洵信 2014-07-16
  • 打赏
  • 举报
回复
引用 1 楼 sagittarius1988 的回复:
不是提示文件找不到么,看看路径
解决方法找到,结贴了,多谢这位大哥的热心帮助
阔活洵信 2014-07-16
  • 打赏
  • 举报
回复
对的。。已然找到方法,贴出来分享。。首先我的程序里面用到了SimpleAdapter 这个类,在SDK源码里面看了一下,里面有一个方法如下:

private void bindView(int position, View view) {
        final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }

        final ViewBinder binder = mViewBinder;
        final String[] from = mFrom;
        final int[] to = mTo;
        final int count = to.length;

        for (int i = 0; i < count; i++) {
            final View v = view.findViewById(to[i]);
            if (v != null) {
                final Object data = dataSet.get(from[i]);
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }

                boolean bound = false;
                if (binder != null) {
                    bound = binder.setViewValue(v, data, text);
                }

                if (!bound) {
                    if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else if (v instanceof TextView) {
                            // Note: keep the instanceof TextView check at the bottom of these
                            // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                            setViewText((TextView) v, text);
                        } else {
                            throw new IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " +
                                    (data == null ? "<unknown type>" : data.getClass()));
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
                }
            }
        }
    }
这里面当控件是ImageView的时候,他只分了两种情况,一种是资源ID,一种是文件地址,我传入的数据是Bitmap类型的数据,在这里当然就当成了文件地址来处理,自然就会有FileNotFoundException。知道了原因,解决方案就呼之欲出了,就是自己重写这个类,增加一种情况当数据类型是Bitmap的时候怎么处理,然后就轻松自在的解决了,代码我就不贴了,因为确实没什么好贴的,找到了原因,解决方案实在不值一提
阔活洵信 2014-07-16
  • 打赏
  • 举报
回复
引用 3 楼 sagittarius1988 的回复:
[quote=引用 2 楼 zxhm001 的回复:] [quote=引用 1 楼 sagittarius1988 的回复:] 不是提示文件找不到么,看看路径
虽然是说文件找不到,但是他找不到的文件是跟的一个内存地址,与路径没有关系。。。你看我的代码里面都写了如果路径有问题会直接跳过 if (!new File(filePath).exists()) { continue; }[/quote] 有时是这样的,文件在,但是你没有权限读取,也会导致这样的错误,文件是在哪的?[/quote] 文件在来着。。。权限也有,可以通过另外的方式查看到。。。我好像已经找到可行的解决方法了,正在试,成功了贴出来。。
sagittarius1988 2014-07-16
  • 打赏
  • 举报
回复
引用 2 楼 zxhm001 的回复:
[quote=引用 1 楼 sagittarius1988 的回复:] 不是提示文件找不到么,看看路径
虽然是说文件找不到,但是他找不到的文件是跟的一个内存地址,与路径没有关系。。。你看我的代码里面都写了如果路径有问题会直接跳过 if (!new File(filePath).exists()) { continue; }[/quote] 有时是这样的,文件在,但是你没有权限读取,也会导致这样的错误,文件是在哪的?
阔活洵信 2014-07-16
  • 打赏
  • 举报
回复
引用 1 楼 sagittarius1988 的回复:
不是提示文件找不到么,看看路径
虽然是说文件找不到,但是他找不到的文件是跟的一个内存地址,与路径没有关系。。。你看我的代码里面都写了如果路径有问题会直接跳过 if (!new File(filePath).exists()) { continue; }
sagittarius1988 2014-07-16
  • 打赏
  • 举报
回复
不是提示文件找不到么,看看路径

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧