FileNotFoundException:downloading attachments

Asked 2 years ago, Updated 2 years ago, 103 views

I would like to download the email attachment via WebView, but I get a FileNotFoundException error.
By the way, the test is Outlook.

code:
ImageGetTask.java

InputStream is;
 String url="https://example.com";
 try{
     URL u = new URL(url);
     HttpURLConnection = (HttpURLConnection) u.openConnection();
     con.setRequestMethod("GET");
     con.setDoOutput(true);
     con.connect();
     int responseCode=con.getResponseCode();
     if(responseCode>=400&&responseCode<=499){
     } else {
        is=con.getInputStream();
        String path=Environment.getExternalStorageDirectory()+"/aproad/";
        String fileName = url.substring(url.lastIndexOf('/')+1);
        File dir = new File (path);
        dir.mkdirs();
        File outputFile=newFile(dir,fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);

        byte [ ] buffer = new byte [1024];
        intlen=0;
        while((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fos.close();
        is.close();

        image=BitmapFactory.decodeStream(is);
     }
     return image;

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

If you change it to this code, you can download it, but it will be saved in Html instead of image format.

Request request = new Request (Uri.parse(url));
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
    request.setVisibleInDownloadsUi(false);
    request.setDescription(contentDisposition);
    downloadManager=(DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    query = new DownloadManager.Query();
    // download flag
    query.setFilterByStatus (DownloadManager.STATUS_FAILED);
    query.setFilterByStatus (DownloadManager.STATUS_SUCCESSFUL);
    downloadid = downloadManager.enqueue(request);

    receiver = new BroadcastReceiver() {
        @ Override
        public void onReceive (Context context, Intent) {
           String action=intent.getAction();
           if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){

               long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
               Log.d("xxx", "End Download download Id="+id);

               query.setFilterById(id);
               Cursorc=downloadManager.query(query);

               if(c.moveToFirst()){
                   int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)));
                   int reason=c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)));
                   Log.d("status", Integer.toString(status)));
                   Log.d("reason", Integer.toString(reason));

                   // If the download fails
                   if(status==DownloadManager.STATUS_FAILED){
                       downloadManager.remove(downloadid);

                   // Successful Download
                   } else if(status==DownloadManager.STATUS_SUCCESSFUL){
                    Log.d(", "SECCUSS");
                    try{
                        ParcelFileDescriptor file=
                                downloadManager.openDownloadedFile(downloadid);
                        FileInputStream files=
                                newParcelFileDescriptor.AutoCloseInputStream(file);
                        imageView.setImageBitmap(BitmapFactory.decodeStream(files));
                    } catch(FileNotFoundExceptione){
                        e.printStackTrace();
                    }

                   }
               }
               c.close();

           }
       }
    };
    registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

java webview exception

2022-09-30 20:33

1 Answers

I was looking into DownloadManager and I got there.
This question is 9 months old, so I don't know if it's already been solved.
I probably had a similar experience before, so I will reply.

Are you trying to retrieve a file that cannot be retrieved without logging in?

When I tried to download the file from WebView to AsyncTask using HttpURLConnection on Android app, it was saved as a jpg file that I specified, but I couldn't open it.
I took the file to my Mac and looked at it in a text editor and found it was retrieved in HTML format.
When I changed the extension and looked at it in my browser, I saw a login form that said, "You need to log in to access it."

I don't know what you're doing with WebView, but in my case, I've already logged in with WebView, so I was able to download the cookie as an image by setting it to HttpURLConnection, so I think you should try it.


2022-09-30 20:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.