Monday, 15 June 2015

Cannot open a file that was created in the temporary folder of an Android app - file does not exist or cannot be read -


When I run the code below, I get a prompt to open the file with the appropriate reader but the file is not displayed .

I'm new to Android app development and any help would be greatly appreciated.

  url = new URL (dwnload_file_path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection (); UrlConnection.setRequestMethod ("GET"); UrlConnection.setDoOutput (true); // connect urlConnection.connect (); File = File.createTempFile ("christo", ".pdf"); FileOutput = New FileOutputStream (file); // Stream used to read data from the internet InputStream inputStream = urlConnection.getInputStream (); // This is the total size of the file that we are downloading total = urlConnection.getContentLength (); RunOnUiThread (New Runnabal) (Public Zero Run) {pb.setMax (totalSize);}}); // Create a buffer ... byte [] buffer = new byte [1024]; Int buffer lamben; While ((buffer length = inputstream. Read (buffer)) gt; 0) {fileOutput.write (buffer, 0, buffer lang); Downloads + = Buffer Langenth; // Update Progress Bar // RunOnUrthread (New Runnabal) (Public Zero Run) {PB.SetProgress (downloaded), Float per = ((downloaded) Size / Total Size) * 100; Qual .set text ("downloaded" + downloaded size + "KB /" + TotalSize + "KB (" + (int +) per + "%)");}}); } // close the output stream when complete // fileOutput.close (); RunOnUiThread (new runnab) {public void run ()} {progressbar not closed}}); } Hold (Last MalformedLELEXception e) {showError ("Error: MalformedURLException" + E); E.printStackTrace (); } Hold (last IOException e) {showError ("Error: IOException" + E); E.printStackTrace (); } Hold (last exception e) {showError ("Error: please check your internet connection" e); } Uri Uri = Uri.framefile (file); Intent = intent (); Intent.setAction (Intent.ACTION_VIEW); Intent.setFlags (Intent.FLAG_GRANT_READ_URI_PERMISSION); Intent.setDataAndType (Yuri, "App / PDF"); StartActivity;  

I think this should be done when creating a file

  file = file. CreateTemplate File ("Christo", "PDF");  

this [dot] is not required


No comments:

Post a Comment