Download Manager cannot download APK - android

I'm trying to download an apk file from the server, but the manager writes waiting for a connection and then writes that the download failed. But this file can be downloaded via Chrome or Retrofit + InputStream. Also i tried to download jpg for test and all works
const val APK_NAME = "test-apk.apk"
val downloadRequest = DownloadManager
.Request(Uri.parse(remoteUpdateConf.newAppStoreUrl))
.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
or DownloadManager.Request.NETWORK_MOBILE
)
.setAllowedOverRoaming(false)
.setTitle(getString(R.string.update_downloading))
.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
)
.setShowRunningNotification(true)
.setVisibleInDownloadsUi(true)
.setMimeType("application/vnd.android.package-archive")
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
APK_NAME
)
downloadManager.enqueue(downloadRequest)

Use this code for download apk using Download Manager:
step 1: Download Apk:
private void DownloadFile(String urlPath, String fileName) {
try {
String OutputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
outputFile = new File(OutputDir, fileName);
if (outputFile.exists()) {
outputFile.delete();
}
OutputFullPATH = outputFile.toString();
// Download File from url
Uri uri = Uri.parse(urlPath + fileName);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("Application Name apk download");
request.setDescription("Downloading Application Name apk");
//Setting the location to which the file is to be downloaded
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
//request.setDestinationInExternalFilesDir(UserAuthenticationActivity.this, Environment.DIRECTORY_DOWNLOADS, fileName + System.currentTimeMillis());
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadID = downloadManager.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(receiver, filter);
//return output;
} catch (Exception e) {
}
}
step 2: After Download if you want to check status
public int getDownloadedStatus() {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(DownloadID);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int columnOndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnOndex);
return status;
}
return DownloadManager.ERROR_UNKNOWN;
}
public void CancelDownload() {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.remove(DownloadID);
}
step 3: Notification for Install
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
try {
ShowProgressBar(false);
Long DownloadedID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (DownloadedID == DownloadID) {
if (getDownloadedStatus() == DownloadManager.STATUS_SUCCESSFUL) {
Toast.makeText(context, "Download Completed", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", new File(OutputFullPATH));
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openFileIntent.setData(contentUri);
startActivity(openFileIntent);
unregisterReceiver(this);
finish();
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(Uri.parse(OutputFullPATH),
"application/vnd.android.package-archive");
startActivity(install);
unregisterReceiver(this);
finish();
}
} else {
Toast.makeText(context, "Download Not Completed", Toast.LENGTH_LONG).show();
}
}
} catch (Exception ex) {
CrashAnalytics.CrashReport(ex);
}
}
};

Related

On progress seekbar notification not showing in DownloadManager android

I am downloading the file from webview. But it's not displaying the on going download notification similar to this when using DownloadManager. It just doing in background operation.
How to display the status bar notification when downloading is in progress.
I can able to get the downloaded file but how to display the on going process in notification?
I used "request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);" but it's not displaying the on going process. Please guide me what mistake I am doing.
Here is my code which I used.
mWebview.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType("pdf");
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
"pdf"));
request.allowScanningByMediaScanner();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//clueless why it's not working..
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
} else {
request.setShowRunningNotification(true);
}
//request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, "pdf"));
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadReference = dm.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, filter);
}
});
}
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadReference == referenceId) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
Cursor c = dm.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// process download
String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + title);//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
//Intent testIntent = new Intent("com.adobe.reader");
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
//testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
try {
startActivity(testIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "No application available to view PDF",
Toast.LENGTH_SHORT).show();
}
// get other required data by changing the constant passed to getColumnIndex
}
}
}
}
};
I checked in Android 7.1.1.
Yes I found solution. The problem is didn't enabled download manager.
Go to the Settings > App
In the options click 'Show System Apps'
Find 'Download Manager' and open it
Click 'Notifications' under App Settings
Switch on Allow Notifications
Reference:
https://stackoverflow.com/a/43841708/1921263

After download how to open downloaded file?

DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.
DIRECTORY_DOWNLOADS, nameOfFile)
To open it
File file = new File(Environment.
DIRECTORY_DOWNLOADS, nameOfFile);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
But I am getting an error message that file cannot be accessed.Check the location
Try using read permission:
android.permission.READ_EXTERNAL_STORAGE
Here is a working solution. Note: Dont use DownloadManager.COLUMN_LOCAL_FILENAME as it is deprecated in API 24. Use DownloadManager.COLUMN_LOCAL_URI instead.
Create fields downlaod manager and a long variable to hold the download id.
DownloadManager dm;
long downloadId;
String pendingDownloadUrl = url;
fial int storagePermissionRequestCode = 101;
Create download manager
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
register the broadcast receiver for download complete
BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
Cursor c = dm.query(new DownloadManager.Query().setFilterById(downloadId));
if (c != null) {
c.moveToFirst();
try {
String fileUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File mFile = new File(Uri.parse(fileUri).getPath());
String fileName = mFile.getAbsolutePath();
openFile(fileName);
}catch (Exception e){
Log.e("error", "Could not open the downloaded file");
}
}
}
};
//register boradcast for download complete
registerReceiver(downloadCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Start the download
Start the download
private void onDownloadStart(String url) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
downloadFile(url);
} else {
pendingDownloadUrl = url;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, storagePermissionRequestCode);
} }
//Download file using download manager
private void downlaodFile(String url){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String filename = URLUtil.guessFileName(url, null, null);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
downloadId = dm.enqueue(request);//save download id for later reference }
//Permission status
#Override public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == storagePermissionRequestCode){
boolean canDownload = true;
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
canDownload = false;
break;
}
}
if(canDownload){
downlaodFile(pendingDownloadUrl);
}
} }
Open the downloaded file
private void openFile(String file) {
try {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(file)), "application/pdf");//this is for pdf file. Use appropreate mime type
startActivity(i);
} catch (Exception e) {
Toast.makeText(this,"No pdf viewing application detected. File saved in download folder",Toast.LENGTH_SHORT).show();
}
}
Now try download your file by calling downladFile(String url); method
Try like this...
protected void openFile(String fileName) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(fileName)),
"MIME-TYPE");
startActivity(install);
}

Why cannot I install an .apk after downloading it

I make an Android App, and i make a feature about update.
I download an .apk file and use intent to install it.But it always has an error like "there was a problem when parsing the package"
my code is
I use a receiver to listen the action when download complete ,code is
private BroadcastReceiver mBroadcaseReceiver;
protected void onCreate(#Nullable Bundle savedInstanceState) {
mCheckUpdateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("AboutUsActivity","check update");
downloadApk();
}
});
mBroadcaseReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
Log.d("aboutusactivity","下载完成");
//下载完毕后安装
installApk();
}
}
};
registerReceiver(mBroadcaseReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
private void downloadApk() {
Log.d("AboutusActivity","update");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("XXXXXX"));
request.setDescription("updating");
request.setTitle("title");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "yuedong.apk");
// 获得下载服务和队列文件
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
private void installApk() {
Intent mIntent = new Intent(Intent.ACTION_VIEW);
mIntent.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS,"yuedong.apk")),
"application/vnd.android.package-archive");
this.startActivity(mIntent);
}
But it always like
So what's wrong with my code?
The app file .apk, that you have downloaded might be corrupted. If you try to install the corrupted apps, you will get the parse error "There was a problem parsing the package". So, try again downloading the app completely and install it.
Its may be because that file having private mode protection(access permission).Try this link.
I know the reason now
because my path which download the apk is not match the path that i choose to install the apk. so stupid i am.
i change it like
private void downloadApk(String url) {
Log.d(TAG,"download");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("updating");
request.setTitle("My app");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
}
request.setDestinationInExternalPublicDir("/xxx/","update.apk");
// 获得下载服务和队列文件
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
private void installApk() {
File mFile;
mFile = new File(Environment.getExternalStorageDirectory()+"/xxx/update.apk");
if (mFile.exists()){
Intent mIntent = new Intent(Intent.ACTION_VIEW);
mIntent.setDataAndType(Uri.parse("file://"+mFile.getAbsolutePath()),
"application/vnd.android.package-archive");
startActivity(mIntent);
}else {
Log.d(TAG,"the file is not exist");
}
}

open file in another app from my apps file directory android

Hi i am downloading a file from URL to my files directory using download manager and then trying to open it with Action_View intent. When i run the Action_View it lets me select quick office and but says can't open file. but when i click on the file from the notification bar it opens fine. Does anyone know how to correctly open a file from an app? what i'm trying to achieve is download a file from url detect when it has finished downloading and allow the user to choose which app to open it in.
heres what i have tried so far
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
ContextWrapper c = new ContextWrapper(getBaseContext());
final String filePath = c.getFilesDir().getPath() + "/";
Log.v("Search", "filePath = " + filePath);
request.setDestinationInExternalFilesDir(getBaseContext(), filePath, filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getBaseContext(), "Downloading...", Toast.LENGTH_LONG).show();
BroadcastReceiver onComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
openFile(filename);
}
protected void openFile(String fileName) {
String path = getFilesDir().getPath() +"/" + fileName;
File f = new File(path);
String Extension = Global.getFileExt(fileName);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
String mimeType = myMime.getMimeTypeFromExtension(Extension);
try {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
intent.setDataAndType(Uri.fromFile(file),mimeType);
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "No handler for this type of file.", 4000).show();
}
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
} else{
}
return false;
}else{
view.loadUrl(url);
return true;
}
}

android: download a pdf file and open after

I'm trying to download a pdf doc from internet and after that, when download ends, open it automatically throught a package.
I found some solutions for downloading by DownloadManager but no answer to open it after. One of the codes I've tested is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri uri = Uri.parse(uriString);
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.xxxxxxx.org/directory/abc.pdf"));
enqueue = dm.enqueue(request);
}
but when the download ends successfully, the package don't find the file.
I need your help. I have spent two weeks looking for a solutions via google and android books with no success.
Thanks.
Create this method and call it in your code
public void showPdf() {
try {
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + name + "CV.pdf");//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
Intent testIntent = new Intent("com.adobe.reader");
testIntent.setType("application/pdf");
testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
startActivity(testIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
Or else you can refer my answer here.
How to use Asynchronous task for download progress dialog in fragment?
Its for downloading file and displaying progress diolog.Here i used asynch task.Call above method in onpost() method of asynch task.
Hope it helps you too

Categories

Resources