简体   繁体   English

较新版本的android API级别4和更高版本上的Filenotfoundexception

[英]Filenotfoundexception on newer versions of android API Level 4 and later

I am working on a project with google maps where i try to retrieve bitmaps from URL and save it to internal memory.After downloading the bitmap into internal memory i try to read it from memory using the following code: 我正在使用谷歌地图进行项目开发,我尝试从URL检索位图并将其保存到内存中。将位图下载到内存中后,我尝试使用以下代码从内存中读取位图:

public Bitmap getImageBitmap(Context context, String name) {

    FileInputStream fis = null;

    try {
        File myFile = new File (path_file + File.separator + name); 
        fis = new FileInputStream(myFile);
        Bitmap b = BitmapFactory.decodeStream(fis);
        return b;
    } catch(Exception e) {
        return null;
    } finally {
        if(fis!=null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

The problem is that the code works fine on Android 2.6 , but it throws Filenotfoundexception at this line 问题是该代码在Android 2.6上可以正常运行,但是在此行抛出Filenotfoundexception

fis = new FileInputStream(myFile);

Why does the code work fine on older versions of android but throws exception on newer versions of android?How do i fix the issue? 为什么代码在旧版本的android上可以正常运行,而在新版本的android上抛出异常?我该如何解决此问题?

EDIT: 编辑:

The issue was with the code which downloads the bitmap: 问题出在下载位图的代码上:

The code that i am using is: 我正在使用的代码是:

public void downloadfile(String path,String filepath)
        {
            try
            {
                URL url = new URL(path);

                URLConnection ucon = url.openConnection();
                ucon.setReadTimeout(5000);
                ucon.setConnectTimeout(10000);
                InputStream is = ucon.getInputStream();
                BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
                File file = new File(filepath);
                file.createNewFile();
                FileOutputStream outStream = new FileOutputStream(file);
                byte[] buff = new byte[5 * 1024];

                int len;
                while ((len = inStream.read(buff)) != -1)
                {
                    outStream.write(buff, 0, len);
                }

                outStream.flush();
                outStream.close();
                inStream.close();

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        }

The code throws NetworkonMainThreadException at this line: InputStream is = ucon.getInputStream(); 的代码抛出NetworkonMainThreadException在这一行: InputStream is = ucon.getInputStream();

This error is thrown only on the newer android version.Please help!! 仅在较新的android版本上引发此错误。请帮助!!

Try this.. 尝试这个..

Just use 只需使用

path_file=MainActivity.this.getFilesDir();

EDIT 编辑

class downloadfile extends AsyncTask<String, Void, Void> {
    protected Void doInBackground(String... urls) {
          try
            {
                URL url = new URL(path);
                URLConnection ucon = url.openConnection();
                ucon.setReadTimeout(5000);
                ucon.setConnectTimeout(10000);
                InputStream is = ucon.getInputStream();
                BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
                File file = new File(filepath);
                file.createNewFile();
                FileOutputStream outStream = new FileOutputStream(file);
                byte[] buff = new byte[5 * 1024];
                int len;
                while ((len = inStream.read(buff)) != -1)
                {
                    outStream.write(buff, 0, len);
                }
                outStream.flush();
                outStream.close();
                inStream.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    }
    protected void onPostExecute() {
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}

Instead if calling downloadfile method use below 相反,如果调用downloadfile方法,请在下面使用

new downloadfile().execute();

You are trying to perform a network related operation in Main thread,you are getting this NetworkonMainThreadException . 您正在尝试在主线程中执行与网络相关的操作,您将获得此NetworkonMainThreadException

Refer to my answer here for more explanation. 请参阅我的答案在这里进行更多的解释。

In your case try downloading the bitmap file in worker thread. 在您的情况下,请尝试在工作线程中下载位图文件。 You can use an Asynctask for it and download bitmap in doInBackground() . 您可以Asynctask使用Asynctask并在doInBackground()下载位图。

Refer this example 参考这个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM