Always FileNotFoundException Permission Denied in android

对着背影说爱祢 提交于 2021-02-05 07:18:25

问题


Why do i always get this error FileNotFoundException Permission Denied? The code is going smooth but when i click a file to download, it wont be downloaded. Please help me. im new to this

Here is my logcat

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

In my manifest

   <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"/>

this is my download function

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

Well, im trying to download an excel file from the server. Each file is displayed as list on which when i click it, it will be downloaded. But Everytime i clicked the file. it outputs the error on the log.cat i gave you


回答1:


If you're using an emulator, you need to set a specific sd card value in your emulator so that you can test and download the file.

If you're using a device then install the apk and then try to download the file. And make sure that the device has a memory card or sd card

So you really need sdcard/memcard to download the file.



来源:https://stackoverflow.com/questions/29312745/always-filenotfoundexception-permission-denied-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!