DownloadManager

Example Code:

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
configure(binding);
}

private void configure(ActivityMainBinding binding) {
final String imageAddress = "https://images.unsplash.com/photo-1707343844152-6d33a0bb32c3?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDF8MHxlZGl0b3JpYWwtZmVlZHwxfHx8ZW58MHx8fHx8";
binding.btnDownload.setOnClickListener(view -> {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imageAddress));
request.setTitle("Image");
request.setDescription("Downloading...");
request.setDestinationUri(Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "image.jpg")));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.allowScanningByMediaScanner();
DownloadManager downloadManager = getSystemService(DownloadManager.class);
downloadManager.enqueue(request);
});
}
}

ImageActivity.java

public class ImageViewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityImageViewBinding binding = ActivityImageViewBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
configure(binding);
}

private void configure(ActivityImageViewBinding binding) {
Uri imageUri = Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "image.jpg"));
binding.imgReview.setImageURI(imageUri);
}
}

DownloadCompleteReceiver

public class DownloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.DOWNLOAD_COMPLETE")) {
DownloadManager downloadManager = context.getSystemService(DownloadManager.class);
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
Uri uri = downloadManager.getUriForDownloadedFile(downloadId);
System.out.println("DownloadCompleteReceiver :: onReceive :: downloadId : " + downloadId);
System.out.println("DownloadCompleteReceiver :: onReceive :: image path : " + uri.getPath());
// Uri.getPath() does not return the actual path of the file downloaded.
Intent imageIntent = new Intent(context, ImageViewActivity.class);
imageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(imageIntent);
}
}
}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DownloadManagerDemo"
tools:targetApi="31">
<receiver
android:name=".DownloadCompleteReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>

<activity
android:name=".ImageViewActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Comments

Popular posts from this blog

SSLSocketFactory in Android

Dagger-Hilt in Android

How to write files in Android?