Skip to content
Advertisement

java.io.FileNotFoundException in Android accessing files on Google Drive

I tried to read the csv file inside Google Drive through the link.
But it’s accessible or impossible. If it return the requestCode, it will also return ‘200’, but in most cases it will return ‘403’.
I thought about it for a few days, but I don’t know why I can’t approach it.
Access to the link through the browser is possible without any problems, but access through the Android studio has occurred with errors.


This is sharing settings for my Google Drive files.

enter image description here
This is my code.

public String getPlaylistData(String selectmood){
        try {
            String pid = "1-5******Ok1iHPVzy2q-Ns"; // url
            HttpsURLConnection urlConnection = null;
            URL stockURL = new URL("https://drive.google.com/uc?export=view&id=" + pid);

            urlConnection = (HttpsURLConnection) stockURL.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            System.out.println("ResponseCode: " + urlConnection.getResponseCode());
            
            // error here!
            BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openConnection().getInputStream())); 

            CSVReader reader = new CSVReader(in);
            String[] nextline;
            Integer j = 0;

            while ((nextline = reader.readNext()) != null) {
                if (!nextline[Category.Playlist_Mood.number].equals(selectmood)) {
                    continue;
                }
                moodselect.add(nextline[Category.Playlist_ID.number]);
            }

            in.close();

            if (urlConnection.getErrorStream() != null){
                try {
                    urlConnection.getErrorStream().close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }



        } catch (Exception e) {
            e.printStackTrace();
        }

        return moodselectid_result;
    }

And this is my logcat.

I/System.out: ResponseCode: 403
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: https://doc-00-2k-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/titjmodt74qf2tnb2i423gujafcd4k2a/1649251650000/05483575271960789860/*/1-5**********E3Ok1iHPVzy2q-Ns?e=view
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
W/System.err:     at com.example.cultureforyou.CSVStreamingActivity.getPlaylistData(CSVStreamingActivity.java:202)
W/System.err:     at com.example.cultureforyou.CSVStreamingActivity.lambda$ThreadNetwork$0$com-example-cultureforyou-CSVStreamingActivity(CSVStreamingActivity.java:162)
W/System.err:     at com.example.cultureforyou.CSVStreamingActivity$$ExternalSyntheticLambda0.run(Unknown Source:4)
W/System.err:     at java.lang.Thread.run(Thread.java:764)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

I tried the following methods. but not works.

1. Added the following settings:

urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
  1. Added the following code (AndroidManifest.xml):
android:usesCleartextTraffic="true"
  1. I changed the code where the error occurs to the code below.
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getURL().openStream()));

Advertisement

Answer

The problem is the URL that you’re using under stockURL.

You seem to be doing a simple fetch with the URL format https://drive.google.com/uc?export=view&id=[file id]. I found this same format on a few websites, including an answer from this very site. It can be used to download or embed files directly from Drive.

The issue is that you’re trying to download a Google Sheets file, which has a different download URL. Instead you have to use https://docs.google.com/spreadsheets/d/[file id]/export. Since you want it as a CSV you can append ?format=csv at the end.

So you can just change your stockURL variable to this:

URL stockURL = new URL("https://docs.google.com/spreadsheets/d/" + pid + "/export?format=csv");

Incidentally, Google Docs files use the URL https://docs.google.com/document/d/[file id]/export. I tried a few other file types and they use the regular Drive URL, but there may be other exceptions. You can check it out yourself by trying to download files from Drive and checking the Network tab in your browser’s Developer tools.

The only strange detail is that you’re getting a 403 response instead of 404, java.io.FileNotFoundException should imply that the file is missing, not that you don’t have permission to access it. I’m not a Java expert but this other answer explains that at least in Spring Security this can happen due to certain settings. Maybe something similar is happening in your case.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement