始终FileNotFoundException权限在android中被拒绝

时间:2015-03-28 01:49:04

标签: android excel download server

为什么我总是收到此错误FileNotFoundException Permission Denied?代码很顺利但是当我点击一个文件下载时,它不会被下载。请帮我。我是新来的

这是我的logcat

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上的错误

1 个答案:

答案 0 :(得分:2)

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

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

所以你真的需要sdcard/memcard来下载文件。