Downloading .cer file from Webview - android

I am wondering since 2 days but not be able to fix it.
I have a URL opened inside on WebView.
there is one Button in website which to download a .cer file .
My problem is when i click on that Button it starts to download file and notified me in Notification Bar.
I am trying below code to do this:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(sourceURL));
// appears the same in Notification bar while downloading
request.setTitle("Downloading");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
//DownloadManager.Request request = new DownloadManager.Request(source);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// Use the same file name for the destination
File destinationFile = new File(directory, "/Download/test.cer");
if (destinationFile.exists()) {
destinationFile.delete();
}
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
referencepath = manager.enqueue(request);
the same is happening with default browser of Device but i am able to downloading file when
i open the url in firefox browser.
i have also added permissions
<!-- Permission so that application can access Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission so that application can write on device storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in Manifest file.
please help me.

Hi finally I succeeded to download .cer file.
The code which worked for me is as:
String sourceURL = url;
String directoryPath = directoryPath;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(sourceURL);
try {
/**
* getting Session Id from URL which is in Webview
*/
String sessionId = CookieManager.getInstance().getCookie(
sourceURL.toString());
if (sessionId != null)
httpPost.addHeader("Cookie", sessionId);
HttpResponse execute = client.execute(httpPost);
String certResponseBody = EntityUtils.toString(execute
.getEntity());
and I got my .cer file as a string in certResponseBody which I saved as a .cer file in device storage.
Cheers....

Related

Downloading file with JSF on Android's WebView

I have a Java Web Application that uses JSF and PrimeFaces. On a backing bean, I download a local pdf or document file:
public void download() throws FileNotFoundException, IOException {
File file = new File("C:/file.pdf");
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) facesContext.getExternalContext().getResponse();
response.reset();
response.setHeader("Content-Type", "application/pdf");
OutputStream responseOutputStream = response.getOutputStream();
InputStream fileInputStream = new FileInputStream(file);
byte[] bytesBuffer = new byte[2048];
int bytesRead;
while ((bytesRead = fileInputStream.read(bytesBuffer)) > 0)
{
responseOutputStream.write(bytesBuffer, 0, bytesRead);
}
responseOutputStream.flush();
fileInputStream.close();
responseOutputStream.close();
facesContext.responseComplete();
}
This is how I call the download method on the xhtml:
<p:commandButton action="#{mybean.download()}" value="Download File" ajax="false" >
And I created a simple Android application for my Java application just by calling it's url inside a WebView:
mWebView.loadUrl("http://myappurl.com");
Then I listen for downloads on the WebView using the DownloadListener event:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Toast.makeText(getApplicationContext(), "Downloading File",
Toast.LENGTH_LONG).show();
}
});
The problem is, the DownloadListener event for WebView does not get called when I press the download button. It works fine by accessing the application URL from the phone's browser, but not in the android application that I created.
It also works on the android application if I put the pdf inside the application directory and call it using a simple <a href="/file.pdf" download>, but that is not what I want, because the pdf files will be on the server's directory and not in the application.
What I've tried:
Using PrimeFaces p:fileDownload - got the same result, nothing happens when button is pressed;
I looked into google docs online viewer, but I cant use that because my files are local;
I tried PDF.js, but this same download button will be used for all kinds of documents, not only pdf, so I don't really need to view the file, I just need to download and save it on the device.
Please help me. Thanks in advance.
EDIT
I tried Chrome Custom Tabs that replaces the Android WebView, it works great, my JSF download was recognized and the file was downloaded, I didn't have to do anything. The only problem is that there is no way of removing the toolbar from Chrome Custom Tabs, because I want my application to seem like it's a native application and not a webpage.

Download Manager not working

I'm trying to develop app that show videos and you can Download it
i'm using Download Manager class but it didn't work, also it didn't give me any error :(
this is my download manager code:
public void downloadFileFromUrl(String url, String fileName) {
String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";
File folder = new File(filePath);
if (!folder.exists()) {
folder.mkdirs();
}
try {
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
DownloadManager downloadManager = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
long id= downloadManager.enqueue(request);
Toast.makeText(this, fileName, Toast.LENGTH_LONG).show();
Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();
}
catch (Exception ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
and this is how I'm calling it
downloadFileFromUrl(path, fileName);
where:
path: "192.168.1.5:8080/BlueNet_NMC/blue_elephant.mp4"
filename: "blue_elephant.mp4"
and i already give this permissions to manifests
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
so please any help
As I said in the comments, DownloadManager only handles requests starting with http:// or https:// as you can see in the docs.
I don't know exactly what's the problem because I lack information about your server, but I think it's a common issue, so you should avoid using an IP address without providing that scheme.
I had a problem when downloading files with an HTTP URL using the DownloadManger class; but then I did the following and the problem was fixed.
Instead of this code:
String url = "http://masteranime.ir/music/best/Dragon Ball GT Dan Dan Kokoro Hikareteku.mp3";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
use this code:
String url = "http://masteranime.ir/music/best/Dragon Ball GT Dan Dan Kokoro Hikareteku.mp3";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url).replaceAll(" ","%20"));
You have problem in this line - request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
Just remove this line or make directory in another way.
request.setDestinationInExternalPublicDir("/BlueNet/", fileName);
You have to mention directory as first argument here. /BlueNet/ is not a directory.

How to store files generated from app in "Downloads" folder of Android?

I am generating an excelsheet in my app which when generated should be automatically saved in the "Downloads" folder of any android device where all the downloads are typically saved.
I have the following which saves the file under "My Files" folder -
File file = new File(context.getExternalFilesDir(null), fileName);
resulting in -
W/FileUtils﹕ Writing file/storage/emulated/0/Android/data/com.mobileapp/files/temp.xls
I rather want to save the generated file automatically in the "Downloads" folder when the excel sheet is generated.
Update # 1: Please see the snapshot here. What I want is the one circled in red and what you suggested gets stored in the one circled blue (/storage/emulated/0/download) if that makes sense. Please advise on how I can save a file in the one circled red i.e., "Downloads" folder which is different from /storage/emulated/0/Download under "MyFiles"
Use this to get the directory:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
And don't forget to set this permission in your manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Now from your question edit I think understand better what you want.
Files shown on the Downloads menu from the one circled in red are ones that are actually downloaded via the DownloadManager, though the previos steps I gave you will save files in your downloads folder but they will not show in this menu because they weren't downloaded. However to make this work, you have to initiate a download of your file so it can show here.
Here is an example of how you can start a download:
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete
request.allowScanningByMediaScanner();// if you want to be available from media players
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
manager.enqueue(request);
This method is used to download from a Uri and i have not used it with a local file.
If your file is not from the internet you could try saving a temporary copy and get the Uri of the file for this value.
Just use the DownloadManager to download your generated file like this:
File dir = new File("//sdcard//Download//");
File file = new File(dir, fileName);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "text/plain",file.getAbsolutePath(),file.length(),true);
The "text/plain" is the mime type you pass so it will know which applications can run the downloadedfile. That did it for me.
I used the following code to get this to work in my Xamarin Droid project with C#:
// Suppose this is your local file
var file = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
var fileName = "myFile.pdf";
// Determine where to save your file
var downloadDirectory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
var filePath = Path.Combine(downloadDirectory, fileName);
// Create and save your file to the Android device
var streamWriter = File.Create(filePath);
streamWriter.Close();
File.WriteAllBytes(filePath, file);
// Notify the user about the completed "download"
var downloadManager = DownloadManager.FromContext(Android.App.Application.Context);
downloadManager.AddCompletedDownload(fileName, "myDescription", true, "application/pdf", filePath, File.ReadAllBytes(filePath).Length, true);
Now your local file is "downloaded" to your Android device, the user gets a notification, and a reference to the file is being added to the downloads folder. Make sure, though, to ask the user for permission before you write to the file system, otherwise an 'access denied' exception will be thrown.
For API 29 and above
According to documentation, it is required to use Storage Access Framework for Other types of shareable content, including downloaded files.
System file picker should be used to save file to External storage directory.
Copy file to external storage example:
// use system file picker Intent to select destination directory
private fun selectExternalStorageFolder(fileName: String) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
putExtra(Intent.EXTRA_TITLE, name)
}
startActivityForResult(intent, FILE_PICKER_REQUEST)
}
// receive Uri for selected directory
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
FILE_PICKER_REQUEST -> data?.data?.let { destinationUri ->
copyFileToExternalStorage(destinationUri)
}
}
}
// use ContentResolver to write file by Uri
private fun copyFileToExternalStorage(destination: Uri) {
val yourFile: File = ...
try {
val outputStream = contentResolver.openOutputStream(destination) ?: return
outputStream.write(yourFile.readBytes())
outputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
As Oluwatumbi have answered this question correctly but First you need to check for permission is granted for WRITE_EXTERNAL_STORAGE by following code:
int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Request for permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}else{
Uri uri = Uri.parse(yourUrl);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete
request.allowScanningByMediaScanner();// if you want to be available from media players
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
manager.enqueue(request);
}
This is running code download files using DownloadManager for Xamarin android api version 29
public bool DownloadFileByDownloadManager(string imageURL)
{
try
{
string file_ext = Path.GetExtension(imageURL);
string file_name = "MyschoolAttachment_" + DateTime.Now.ToString("yyyy MM dd hh mm ss").Replace(" ", "") + file_ext;
var path = global::Android.OS.Environment.DirectoryDownloads;
Android.Net.Uri uri = Android.Net.Uri.Parse(imageURL);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.SetDestinationInExternalPublicDir(path, file_name);
request.SetTitle(file_name);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted); // to notify when download is complete
// Notify the user about the completed "download"
var downloadManager = DownloadManager.FromContext(Android.App.Application.Context);
var d_id = downloadManager.Enqueue(request);
return true;
}
catch (Exception ex)
{
UserDialogs.Instance.Alert("Error in download attachment.", "Error", "OK");
System.Diagnostics.Debug.WriteLine("Error !" + ex.ToString());
return false;
}
}
WORKING ON API 29 AND 30
To get the directory:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Set this permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Add requestLegacyExternalStorage to your application tag in AndroidManifest.xml (for API 29):
<application
...
android:requestLegacyExternalStorage="true">

Can download, can't read

I'm using this to download a file:
String url = "https://File To Download";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("My File To Download");
request.setTitle("Downloading");
// 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);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, context.getString(R.string.dloded_latest_numbers));
// get download service and enqueue file
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Which all works great. Puts the file in the standard Downloads directory which I confirm is there.
Then I try to read the file into a BufferedReader like so:
File latestNumbersFile;
latestNumbersFile = new File(Environment.DIRECTORY_DOWNLOADS, getString(R.string.dloded_latest_numbers));
TextView displayArray = (TextView) findViewById(R.id.currentNumbers);
String tmp = latestNumbersFile.toString(); // Converts type (File) to string type
displayArray.setText(tmp);
BufferedReader buffedReader = new BufferedReader(new FileReader(latestNumbersFile));
Then display the file location in a TextView, which works and gives the dir /Download/File To Download
Note the leading "/" not sure if that's important...
My question is:
Using File(Environment.DIRECTORY_DOWNLOADS, getString(R.string.dloded_latest_numbers) allows me to download to a dir I expect the file to be in, but using the exact same reference to read the file from I get java.io.FileNotFoundException open failed: ENOENT (No such file or directory)
Can anyone help??
ok you have only asked for android.permission.WRITE_EXTERNAL_STORAGE permission but you dont have the permission to read from the SD Card. you also need to add
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Android DownloadManager "Impossible to open file"

I'm using DownloadManager to download a file from a webService. The download end successfuly, but when I try to open the new file in the "download" folder I've got this error "Impossible to open file" (and I know I can open this type of file).
Also, when I plug my phone to my computer and when I open the download file with it, the file open successfuly and is not corrupted.
I don't have other error, so I'm really lost !
Here my code:
/*Data*/
int filePosition = position - _subFolderNameList.length;
String url = _folder.getFiles().get(filePosition).getUrl();
String FileName = _folder.getFiles().get(filePosition).getName();
String Description = _folder.getFiles().get(filePosition).getUrl();
/*Prepare request*/
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(Description);
request.setTitle(FileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request); // Send request
Edit: Permission in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Okay I found the problem !
I had to specify the "MIME content type" of the file using setMimeType().
public DownloadManager.Request setMimeType (String mimeType);
Yes you have to specify the MIME type.
Adding a few more details to the accepted answer.
public DownloadManager.Request setMimeType (String mimeType);
To get MIME type you can use the following function.
private String getMimeFromFileName(String fileName) {
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(fileName);
return map.getMimeTypeFromExtension(ext);
}
and the following is the Xamarin.Android implementation of the same :
private static string GetMimeTypeFromFileName(string fileName)
{
var map = MimeTypeMap.Singleton;
var ext = MimeTypeMap.GetFileExtensionFromUrl(fileName);
return map.GetMimeTypeFromExtension(ext);
}

Categories

Resources