0

I had a few problems with this initially due to not requesting the permissions for writing at run time but I have resolved that issue seemingly.The code grabs a pdf file from a website and is supposed to store it in the app directory,I have set the post execute to set a textView to the intended directory for the created file.No file is created and no exceptions are hit so I am pretty stumped here

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final int REQUEST_WRITE_PERMISSION = 786;

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        new FetchWebsiteData(this).execute();
    }
}
private void requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
    } else {
        //do something
        //new FetchWebsiteData().execute();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnFetchData = (Button) findViewById(R.id.buttonTest);
    btnFetchData.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            requestPermission();


        }
    });

}



private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {

    private String pdfLink = "didnt work";
    private Link foundLink = new Link("");
    String fileLocation= "";
    Context context;
    public FetchWebsiteData(Context context1){
        context = context1;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected Void doInBackground(Void... params) {
        int count;
        try {

            Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
            //Elements links = doc.select("a[title=\"Download offers in store\"]");
            Element links = doc.select("a[title=\"Download offers in store\"]").first();
            foundLink = new Link(links.attr("href"));

            URL url = new URL(foundLink.getUrlWithDomain());


            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);

            //and connect!
            urlConnection.connect();


            File file = new File(context.getFilesDir(),foundLink.getFileNameOnly());
            fileLocation = file.toString();
            //this will be used to write the downloaded data into the file we created
            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            //variable to store total downloaded bytes
            int downloadedSize = 0;

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //this is where you would do something to report the prgress, like this maybe


            }
            //close the output stream when done
            fileOutput.close();





        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        TextView txttitle = (TextView) findViewById(R.id.resultTextView);
        //testing the file location
        txttitle.setText(fileLocation);

    }

}
Daniel
  • 199
  • 2
  • 4
  • 18
  • "No file is created" -- how **exactly** have you determined this? `adb shell ls`? An on-device file manager? Android Debug Monitor? A desktop OS's file manager? Something else? – CommonsWare Dec 06 '16 at 22:01
  • Sorry I am running it on my own android device and just using windows explorer on my desktop through USB – Daniel Dec 06 '16 at 22:03

1 Answers1

0

You are writing to internal storage. This cannot be viewed by the user using a desktop OS file manager, or much of anything else, for that matter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Great thank,I guess I misunderstood the meaning of internal storage.Would there be a way to write it so that it could be viewed by a desktop file manager? – Daniel Dec 06 '16 at 22:08
  • @Daniel: Not on internal storage. For that, you would use [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html). And, you would have to add a bit of code to [arrange for those files to be picked up quickly by `MediaStore`](http://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl) for them to be visible from Windows right away. – CommonsWare Dec 06 '16 at 22:10