尽管文件存在,但Drawable.createFromPath(filePath)返回null [英] Drawable.createFromPath(filePath) returning null although the file exist

查看:825
本文介绍了尽管文件存在,但Drawable.createFromPath(filePath)返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

String filePath = getActivity().getFileStreamPath("into11.png").getAbsolutePath();
        if(Drawable.createFromPath(filePath)!=null){
        img.setImageDrawable(Drawable.createFromPath(filePath));
        }
        else{
        Log.d("nulllllllllllllllllllllllllllllllll","yessssssssssssssssssssssssssss");  
        }

文件路径正在获取图像"data/data/.../intro11.png"的正确路径,但是Drawable.createFromPath(filePath)为null,这是什么原因?

the filepath is getting the right path of the image "data/data/.../intro11.png" but Drawable.createFromPath(filePath) is null so what's the cause of this?

推荐答案

我遇到了同样的问题,对我来说,我试图从以前保存到内部存储的文件中创建一个Drawable.我的问题最终是我的文件被错误地保存,因此没有创建可绘制对象.它还在调试控制台中引发了错误.

I had this same problem and for me I was trying to make a Drawable from a file that I had previously saved to internal storage. My problem ended up being that my file was incorrectly saved so it didn't create the drawable. It also threw errors in the debug console.

我希望这对某人有帮助.

I hope this helps somebody.

这是我要保存的代码(被注释掉的位是我所缺少的):

This was my code to save (The bit commented out is what I was missing):

URL url = new URL(remoteFile);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
FileOutputStream fos = mainActivity.openFileOutput(filename,            
Context.MODE_PRIVATE);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
byte[] byteArray = outStream.toByteArray();
fos.write(byteArray);
fos.close();

我仍然想知道只是逐字节保存文件,所以我不必打开图像并重新压缩它.

I'd still like to know to just save files byte by byte so I don't have to open the image and recompress it.

这篇关于尽管文件存在,但Drawable.createFromPath(filePath)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆