Widget Recent Post No.

header ads

How to download file from url in Android Studio


Step 1 MAIN.XML


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">

    <EditText

        android:layout_width="match_parent"

        android:hint="Enter Link Of Pdf"

        android:layout_margin="20dp"

        android:id="@+id/pdf"

        android:layout_height="100dp" />

    <EditText

        android:layout_width="match_parent"

        android:hint="Enter Title Of Pdf"

        android:layout_margin="20dp"

        android:id="@+id/title"

        android:layout_height="50dp" />

    <Button

        android:layout_width="match_parent"

        android:text="Start Downloading"

        android:layout_margin="50dp"

        android:onClick="StartDownloading"

        android:layout_height="wrap_content" />

</LinearLayout>


Step 2 MAINJAVA

package in.learncodewithrk.demo;


import androidx.appcompat.app.AppCompatActivity;



import android.app.DownloadManager;

import android.content.Context;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.EditText;



public class MainActivity extends AppCompatActivity  {




    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



    }

    public void StartDownloading(View view){

        EditText editText=findViewById(R.id.pdf);

        EditText title=findViewById(R.id.title);

        String title1=title.getText().toString();

        String url=editText.getText().toString();

        DownloadBooks(url,title1);


    }

    public  void DownloadBooks(String url,String title){

        DownloadManager.Request request=new DownloadManager.Request(Uri.parse(url));

        String tempTitle=title.replace("","_");

        request.setTitle(tempTitle);

        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB){

            request.allowScanningByMediaScanner();

            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        }

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,tempTitle+".pdf");

        DownloadManager downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);

        request.setMimeType("application/pdf");

        request.allowScanningByMediaScanner();

        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);

        downloadManager.enqueue(request);


    }

}


GITHUB : https://github.com/LearncodeWithRk/download-pdf

 

Post a Comment

0 Comments