Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android API 4级及更高版本上的Filenotfoundexception_Java_Android_Eclipse_Google Maps - Fatal编程技术网

Java android API 4级及更高版本上的Filenotfoundexception

Java android API 4级及更高版本上的Filenotfoundexception,java,android,eclipse,google-maps,Java,Android,Eclipse,Google Maps,我正在google maps的一个项目中工作,我尝试从URL检索位图并将其保存到内存中。将位图下载到内存中后,我尝试使用以下代码从内存中读取位图: public Bitmap getImageBitmap(Context context, String name) { FileInputStream fis = null; try { File myFile = new File (path_file + File.separator + name);

我正在google maps的一个项目中工作,我尝试从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();
            }
        }
    }
}
问题是该代码在Android2.6上运行良好,但在这一行抛出Filenotfoundexception

fis=新文件输入流(myFile)

为什么代码在旧版本的android上运行良好,但在新版本的android上抛出异常?我如何解决这个问题

编辑:

问题在于下载位图的代码:

我使用的代码是:

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

        }
代码在此行抛出
networkonmainthreadeception
InputStream is=ucon.getInputStream()

此错误仅在较新的android版本上引发。请帮助

试试这个

只用

path_file=MainActivity.this.getFilesDir();
编辑

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
    }
}

您正试图在主线程中执行与网络相关的操作,您将获得此
NetworkonMainThreadException

更多解释请参考我的答案

在您的情况下,请尝试在工作线程中下载位图文件。您可以对其使用
Asynctask
,并在
doInBackground()
中下载位图


请参阅

请打印此行
System.out.println(path\u file+file.separator+name)
path_file=MainActivity.this.getFilesDir().getAbsolutePath();请参阅我的答案。@user3852672使用异步任务在doinbackground中下载文件方法。
new downloadfile().execute();