cijo
cijo

Reputation: 23

How to download and store pdf file in a folder in Android 10?

Until Android pie my code was working fine. But when I update to Android 10, it shows following errors. How to sort it out?

E/SubsamplingScaleImageView: Failed to initialise bitmap decoder
    java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
        at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:315)
        at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:220)
        at com.pdfview.PDFRegionDecoder.init(PDFRegionDecoder.kt:25)
        at com.pdfview.subsamplincscaleimageview.SubsamplingScaleImageView$TilesInitTask.doInBackground(SubsamplingScaleImageView.java:1564)
        at com.pdfview.subsamplincscaleimageview.SubsamplingScaleImageView$TilesInitTask.doInBackground(SubsamplingScaleImageView.java:1539)
        at android.os.AsyncTask$3.call(AsyncTask.java:378)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

I am Using following code.

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "tmf");
folder.mkdir();

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(folder + "/contract.pdf");

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
    total += count;
    publishProgress(""+(int)((total*100)/lenghtOfFile));
    output.write(data, 0, count);
}

output.flush();
output.close();
input.close();`

Upvotes: 2

Views: 4851

Answers (3)

Ak23
Ak23

Reputation: 398

Try Below code.

String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "filename.pdf";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);

String url = downloadurl; //paste url here

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading....");
request.setTitle(" TITLE ");
request.setDestinationUri(uri);

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);

final String finalDestination = destination;
BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Log.d("Update status", "Download completed");
       unregisterReceiver(this);
    }
};

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Upvotes: 1

sujith
sujith

Reputation: 2421

If your app is targetting Q, you can't directly write to External public storage. You have to use MediaStore to save the files to a publicly accessible location.

You can use the following reference code to store a file using MediaStore. Note that You need android.permission.WRITE_EXTERNAL_STORAGE to store a file using MediaStore.

    val cv = ContentValues().apply {
        put(MediaStore.Files.FileColumns.DISPLAY_NAME, "mypdf.pdf")
    }
    val uri = contentResolver.insert(MediaStore.Files.getContentUri("pdfs"), cv)

    uri?.let { 
        val out = contentResolver.openOutputStream(it)
    }

Upvotes: 2

Talha Ahmed
Talha Ahmed

Reputation: 66

Have you taken permissions for read external storage , also the error states file not found make sure the file is being created

Upvotes: 0

Related Questions