为什么我总是收到此错误FileNotFoundException权限被拒绝?代码运行顺利,但是当我单击文件下载时,将不会下载。请帮我。我是新来的

这是我的日志

03-28 09:19:34.695: E/log_tag(17921): eer java.io.FileNotFoundException: /mnt/sdcard/Excel.xlsx (Permission denied)


在我的清单上

   <uses-sdk android:minSdkVersion="11"
          android:targetSdkVersion="15"
          android:maxSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


这是我的下载功能

//Download
    public void startDownload(final int position) {

        Runnable runnable = new Runnable() {
            int Status = 0;

            public void run() {

                String urlDownload = MyArrList.get(position).get("FileUrl").toString();
                int count = 0;
                try {
                      Log.d("", urlDownload);
                    URL url = new URL(urlDownload);
                    URLConnection conexion = url.openConnection();
                    conexion.connect();

                    int lenghtOfFile = conexion.getContentLength();
                    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                    InputStream input = new BufferedInputStream(url.openStream());

                    // Get File Name from URL
                    String fileName = urlDownload.substring(urlDownload.lastIndexOf('/')+1, urlDownload.length() );
                //Environment.getExternalStorageDirectory().getPath()
                    Log.d("", fileName);
                    OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+fileName);

                    byte data[] = new byte[1024];
                    long total = 0;

                        while ((count = input.read(data)) != -1) {
                            total += count;
                            Status = (int)((total*100)/lenghtOfFile);
                            output.write(data, 0, count);

                            // Update ProgressBar
                            handler.post(new Runnable() {
                                public void run() {
                                    updateStatus(position,Status);
                                }
                            });

                        }

                        output.flush();
                        output.close();
                        input.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("log_tag", "eer "+e.toString());

                    }


            }
        };
        new Thread(runnable).start();
    }


好吧,即时通讯试图从服务器上下载一个excel文件。每个文件都显示为列表,当我单击该列表时,将下载该文件。但是每次我单击文件时。它在我给你的log.cat上输出错误

最佳答案

如果使用的是emulator,则需要在模拟器中设置特定的sd card value,以便可以测试和下载文件。

如果您使用的是设备,请先安装apk,然后尝试下载文件。并确保设备具有memory cardsd card

因此,您确实需要sdcard/memcard来下载文件。

07-27 19:17