简体   繁体   中英

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:

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

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?

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

This error is thrown only on the newer android version.Please help!!

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

new downloadfile().execute();

You are trying to perform a network related operation in Main thread,you are getting this 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() .

Refer this example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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