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
Related
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
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);
}
What i am having ?
Here patientAttachment.getFile_path() have the link of the url that has image
.
vObjPatientMedication.downloadId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse(patientAttachment.getFile_path()));
activity.startActivity(myWebLink);
}
});
What now is happening: i am displaying the image in browser
What i am trying to achieve: i want to initiate the download of that file(file can be pdf/image/word) to any default location where downloading is taken care by android.
How to achieve this ?
You can use DownloadManager:
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uriString));
downloadManager.enqueue(request);
You should also register a receiver to know when a download completes:
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
//Your code
}
}
};
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;
}
}
I have an Intent class to send multiple images to the email as an attached file. So all works fine. Only issue is that when no of images is more than 20 then it The Intent class takes time to open a particular mail client. So during that time i want to show progress bar to the user. So anyone help me to solve this out. My code for sending multiple images is shown below.
ArrayList<Uri> uris = new ArrayList<Uri>();
for(int i = 0; i< NoteManager.getSingletonObject().getNoteItemCount(); i++)
{
File imageFile = new File(m_ShareDir, NoteManager.getSingletonObject().getNote(i));
ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.DATA, imageFile.getAbsolutePath());
Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
uris.add(imageUri);
}
Intent intent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/png");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));
May it helps you
ProgressDialog pd = ProgressDialog.show(SetFrames.this,"Loading", "Please
Wait...",true);
new Thread() {
public void run() {
try {
//do your process
}catch(Exception e)
{
}
handler.sendEmptyMessage(0);
pd.dismiss();
}
}.start();
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
}
};
Try this. tutorial. And I think here is good example for you which show the progress item one by one.
Enjoy...