How to download any image , video , audio and pdf file from direct URL to into your internal storage
Downloading file from URL is the most common task in any application .here we are using download manager to download URL file image , video , audio and pdf content from server to your internal storage
Step 1: add permission to of internal storage into manifest file and add requestlegacyexternalstorage to true if you are using android 10 or above
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
</application>
Step 2:add edit text and button into your xml file
<EditText
android:id="@+id/txt_url"
android:hint="Paste url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></EditText>
<Button
android:onClick="fun_download"
android:text="Download"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></Button>
Step 3: get your URL from edit text and then call fun_download on button click
public class Download_files_from_url extends AppCompatActivity {
EditText txturl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_files_from_url);
txturl=findViewById(R.id.txt_url);
requsetpermission();
checkpermissionformdevice();
}
public void fun_download(View view) {
String url=txturl.getText().toString().trim();
DownloadManager.Request request=new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);
request.setTitle("Downloading..");
request.setDescription("File is downloading please Wait");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,""+System.currentTimeMillis());
DownloadManager manager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
private void requsetpermission() {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
}, 0);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 2:
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
}
}break;
}
}
private boolean checkpermissionformdevice() {
int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
return write_external_storage_result == PackageManager.PERMISSION_GRANTED && record_audio_result == PackageManager.PERMISSION_GRANTED;
};
}
<
Comments
Post a Comment