When I try to download an image from web view
Download starts but the image isn't saved in gallery .. " I don't know where"
and when I delete the cache of download manager it disappears what is the problem with the code?
package com.example.misrcomp.myapplication;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 10;
public android.widget.ProgressBar ProgressBar;
#Override
public void onCreateContextMenu(ContextMenu contextMenu,View view,ContextMenu.ContextMenuInfo contextMenuInfo){
super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
final WebView.HitTestResult webViewHitTestResult = mWebView.getHitTestResult();
if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
contextMenu.setHeaderTitle("Download Image From Below");
contextMenu.add(0, 1, 0, "Save - Download Image")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
String DownloadImageURL = webViewHitTestResult.getExtra();
if(URLUtil.isValidUrl(DownloadImageURL)){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Toast.makeText(MainActivity.this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).show();
}
return false;
}
});
}
}
private Context mContext;
private Activity mActivity;
private LinearLayout mRootLayout;
private WebView mWebView;
private static final int MY_PERMISSION_REQUEST_CODE = 123;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this,"By:Hisham Raafat",Toast.LENGTH_SHORT).show();
}
},SPLASH_TIME_OUT);
ProgressBar = (ProgressBar) findViewById(R.id.prg);
// Get the application context
mContext = getApplicationContext();
mActivity = MainActivity.this;
// Get the widget reference from xml layout
mRootLayout = findViewById(R.id.root_layout);
mWebView = findViewById(R.id.web_view);
// Check permission for write external storage
checkPermission();
// The target url to surf using web view
String url = "http://nekashatspacetoon.yoo7.com/h29-h";
registerForContextMenu(mWebView);
// Load the url in web view
mWebView.loadUrl(url);
mWebView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view,String url,Bitmap favicon) {
ProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view,String url) {
ProgressBar.setVisibility(view.GONE);
setTitle(view.getTitle());
}
}
);
// Enable java script on web view
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDescription,
String mimetype, long contentLength) {
/*
DownloadManager.Request
This class contains all the information necessary to request a new download.
The URI is the only required parameter. Note that the default download
destination is a shared volume where the system might delete your file
if it needs to reclaim space for system use. If this is a problem,
use a location on external storage (see setDestinationUri(Uri).
*/
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
/*
void allowScanningByMediaScanner ()
If the file to be downloaded is to be scanned by MediaScanner, this method
should be called before enqueue(Request) is called.
*/
request.allowScanningByMediaScanner();
/*
DownloadManager.Request setNotificationVisibility (int visibility)
Control whether a system notification is posted by the download manager
while this download is running or when it is completed. If enabled, the
download manager posts notifications about downloads through the system
NotificationManager. By default, a notification is shown only
when the download is in progress.
It can take the following values: VISIBILITY_HIDDEN, VISIBILITY_VISIBLE,
VISIBILITY_VISIBLE_NOTIFY_COMPLETED.
If set to VISIBILITY_HIDDEN, this requires the permission
android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
Parameters
visibility int : the visibility setting value
Returns
DownloadManager.Request this object
*/
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
/*
DownloadManager
The download manager is a system service that handles long-running HTTP
downloads. Clients may request that a URI be downloaded to a particular
destination file. The download manager will conduct the download in the
background, taking care of HTTP interactions and retrying downloads
after failures or across connectivity changes and system reboots.
*/
/*
String guessFileName (String url, String contentDisposition, String mimeType)
Guesses canonical filename that a download would have, using the URL
and contentDisposition. File extension, if not defined,
is added based on the mimetype
Parameters
url String : Url to the content
contentDisposition String : Content-Disposition HTTP header or null
mimeType String : Mime-type of the content or null
Returns
String : suggested filename
*/
String fileName = URLUtil.guessFileName(url,contentDescription,mimetype);
/*
DownloadManager.Request setDestinationInExternalPublicDir
(String dirType, String subPath)
Set the local destination for the downloaded file to a path within
the public external storage directory (as returned by
getExternalStoragePublicDirectory(String)).
The downloaded file is not scanned by MediaScanner. But it can be made
scannable by calling allowScanningByMediaScanner().
Parameters
dirType String : the directory type to pass to
getExternalStoragePublicDirectory(String)
subPath String : the path within the external directory, including
the destination filename
Returns
DownloadManager.Request this object
Throws
IllegalStateException : If the external storage directory cannot be
found or created.
*/
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,fileName);
DownloadManager dManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
/*
long enqueue (DownloadManager.Request request)
Enqueue a new download. The download will start automatically once the
download manager is ready to execute it and connectivity is available.
Parameters
request DownloadManager.Request : the parameters specifying this download
Returns
long : an ID for the download, unique across the system. This ID is used
to make future calls related to this download.
*/
dManager.enqueue(request);
}
});
}
protected void checkPermission(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
// show an alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setMessage("Write external storage permission is required.");
builder.setTitle("Please grant permission");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(
mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST_CODE
);
}
});
builder.setNeutralButton("Cancel",null);
AlertDialog dialog = builder.create();
dialog.show();
}else {
// Request permission
ActivityCompat.requestPermissions(
mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST_CODE
);
}
}else {
// Permission already granted
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch(requestCode){
case MY_PERMISSION_REQUEST_CODE:{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// Permission granted
}else {
// Permission denied
}
}
}
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
Just type this in onMenuItemClick under String DownloadImageURL:
mWebView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDescription,
String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,fileName);
DownloadManager dManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dManager.enqueue(request);
}
});
}
Related
So I am stuck on a issue with my webapp that runs inside a webview from android studio.
I want download like pdf file to be downloaded and opend in the native app on the phone. This work fine by using the download manager from android studio.
How ever I also have links that start with "mailto:" and "tel:" those links give me an error when I don't override the method "shouldOverrideUrlLoading" where I can check what kind of url it is. And then open the propper inten.
So when I combine the 2 the downloadmanager and the custom NavigationHandler that extends the WebViewClient, it doesn't work as expected.
For a better understanding of what is happening.
When I hit a button with a pdf file it downloads the file gives a toast message and opens the file with the native app on the phone. This is without overriding the "shouldOverrideURLLoading" and without my class that extends the WebViewClient.
When I also use my own NavigationHandler witch extends from WebViewClient,
my urls with "mailto:" and "tel:" open with native apps on the phone.
When I now hit a button with a pdf file it is opend in a browser to be downloaded. Witch I don't want. I have tried numerous things to solf the problem but until now without succes.
I run a website app inside of a WebViewClient.
P.S. sorry for the shitty code but it's new to me and haven't find my way jet in coding in Android Studio.
My NavigationHandler class
package nl.firejob.selector;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class NavigationHandler extends WebViewClient {
private static final String TEL_PREFIX = "tel:";
private static final String MAILTO_PREFIX = "mailto:";
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ( url.startsWith( TEL_PREFIX ) ) {
// This is a tel link, which should be opened with the native thing.
Intent tel = new Intent( Intent.ACTION_DIAL, Uri.parse( url ) );
view.getContext().startActivity( tel );
return true;
} else if ( url.startsWith( MAILTO_PREFIX ) ) {
// This is a mail link, which should be opened with the other native thing.
Intent mail = new Intent(Intent.ACTION_SENDTO);
mail.setType("message/rfc822");
mail.setData(Uri.parse( url ));
view.getContext().startActivity( mail );
return true;
} else if ( Uri.parse(url).getHost().startsWith("myurl.com") ) {
// This is what we want to show in the app, so let the WebView handle it.
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( url ) );
view.getContext().startActivity( intent );
return true;
}
}
My MainActivity Class
package nl.firejob.selector;
import android.Manifest;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private DownloadManager dm;
private Long myDownloadReference;
private BroadcastReceiver receiveDownloadComplete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
// Allow webview to use javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Stop local links/redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new NavigationHandler() {
#Override
public void onPageFinished(WebView view, String url) {
// Show the webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
// Hide splashscreen objects
findViewById(R.id.imageLogo).setVisibility(View.GONE);
findViewById(R.id.textLogo).setVisibility(View.GONE);
}
});
mWebView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
if( haveStoragePermission()) {
Log.i("download url",url);
//for downloading directly through download manager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setVisibleInDownloadsUi(true);
request.setDescription("Doorvoerboek").setTitle("doorvoerboek.pdf");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "doorvoerboek.pdf");
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
myDownloadReference = dm.enqueue(request);
IntentFilter intentFilter = new IntentFilter( dm.ACTION_DOWNLOAD_COMPLETE);
receiveDownloadComplete = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (myDownloadReference == reference) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(reference);
Cursor cursor = dm.query(query);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int fileNameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TITLE);
String saveFilePath = cursor.getString(fileNameIndex);
Log.i("filename",saveFilePath);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
switch (status){
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(MainActivity.this, "Download Complete", Toast.LENGTH_LONG).show();
Log.i("dir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() );
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() +"/doorvoerboek.pdf");
Intent intentView = new Intent(Intent.ACTION_VIEW);
intentView.setDataAndType(Uri.fromFile(file),"application/pdf");
intentView.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intentView);
break;
}
}
}
};
registerReceiver(receiveDownloadComplete,intentFilter);
}
}
});
mWebView.loadUrl("http://myurl.com/");
}
public boolean haveStoragePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.e("Permission error","You have permission");
return true;
} else {
Log.e("Permission error","You have asked for permission");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //you dont need to worry about these stuff below api level 23
Log.e("Permission error","You already have the permission");
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
//if Back key pressed and webview can navigate to previous page
mWebView.goBack();
// go back to previous page
return true;
}
else
{
finish();
// finish the activity
}
return super.onKeyDown(keyCode, event);
}
}
This code downloads any file.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
spinner = (ProgressBar) findViewById(R.id.progressBar1);
webview.setWebViewClient(new CustomWebViewClient());
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("http://www.website.com");
//Download file code stackoverflow.com
webview.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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "downloading",
Toast.LENGTH_LONG).show();
}
});
// Download section of code
} // Close of onCreate
// mailto code stackoverflow.com
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
}
else if (url.startsWith("mailto:")) {
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent mail = new Intent(Intent.ACTION_SEND);
Intent intent = mail.setType("application/octet-stream");
MailTo recipient = MailTo.parse(url);
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient.getTo()});
mail.putExtra(Intent.EXTRA_SUBJECT, "Contact");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
}
return true;
}
}
// mailto section of code
I am new here and reason for that I don't know all rules of the site yet, sorry about that!
I am building a Java application to Desktop and Android by using Gluon mobile plugin in Eclipse tool. I have variated code for Desktop and Android as described in an example (http://docs.gluonhq.com/samples/gonative/). So I have Android specific NativeService where I have a method to read files from external storage (that works). I have also studied how to request permissions on run time when using native Android environment (Storage permission error in Marshmallow ). I am able to request the permission, but I can not catch the result.
I am now requesting the permissions same way, but how I can receive the results? I cannot overrrive onRequestPermissionsResult as in the example, becauce I have not directly available android.support.v4.app stuff. Could someone help me and provide an example?
AndroidNativeService.java:
package com.tentaattorix;
import java.io.IOException;
import java.io.File;
import android.os.Environment;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.content.Context;
import javafxports.android.FXActivity;
import android.util.Log;
import android.widget.Toast;
import android.content.pm.PackageManager;
import android.os.Build;
import com.avustajat.LueJaKirjoita;
/**
* Sanaston luku: Android-natiivi toteutus!
* #author PT
* #version 24.10.2016
*
*/
public class AndroidNativeService implements NativeService {
private static final String TAG = "Tentaattorix Native Service";
private Sanastot sanastot = new Sanastot();
private static final int MY_PERMISSIONS_USE_EXTERNAL_STORAGE = 1;
public AndroidNativeService() {
//
}
#Override
public Sanastot haeSanastot(String juuriKansio, String sanastoRegex, char kommentti, char erotin) throws IOException {
String polku = Environment.getExternalStorageDirectory().toString();
String readme = "LueMinut.txt";
String kansioPolku ="";
//Luodaan kansio, jos sitä ei vielä ole.
File kansio = new File(polku, juuriKansio);
kansio.mkdir();
//Asetetaan oikeudet, jos vaikka auttais skannaukseen.
kansio.setWritable(true);
kansio.setReadable(true);
kansio.setExecutable(true);
kansioPolku = kansio.getAbsolutePath();
//Kysy oikeudet, jos niitä ei ole!
if (isStoragePermissionGranted()) {
//Luodaan kansioon tiedosto LueMinut.txt.
try {
LueJaKirjoita.luoLueMinut(kansioPolku, readme);
}
catch (IOException e){
throw e;
}
//Informoidaan uudesta kansiosta ja sinne tulevista tiedostoista järjestelmää!
scanFile(kansioPolku + File.separator + readme);
//Luetaan sanastot kansiosta.
sanastot = LueJaKirjoita.lueTiedostot(kansioPolku, sanastoRegex, kommentti, erotin);
}
// Jos sanastot ei sisällä yhtään sanastoa,
// niin laitetaan edes yksi :)
if (sanastot.annaLkm() < 1) {
String[] rivix = {"Tyhjä sanasto!", "Empty glossary!"};
Sanasto san = new Sanasto("sanasto_");
san.lisaa(rivix);
sanastot.lisaa(san);
}
return sanastot;
}
/**
* //Informoidaan uudesta kansiosta ja sinne tulevista tiedostoista järjestelmää!
* #param path lisätty polku+tiedosto
*/
private void scanFile(String path) {
MediaScannerConnection.scanFile(FXActivity.getInstance().getApplicationContext(),
new String[] { path }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("TAG", "Finished scanning " + path);
}
});
}
private boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (FXActivity.getInstance().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
FXActivity.getInstance().requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_USE_EXTERNAL_STORAGE);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
}
For starters, you can add the android-support-v4.jar to your project:
Copy it from its location under ANDROID_HOME/extras/android/support/v4/android-support-v4.jar to a libs folder in your project, and then add the dependency to the build.gradle file:
dependencies {
androidCompile files('libs/android-support-v4.jar')
}
Assuming you are targeting Android SDK 23+:
android {
minSdkVersion '23'
compileSdkVersion '23'
targetSdkVersion '23'
manifest = 'src/android/AndroidManifest.xml'
}
then you know that by default all the permissions included in the manifest will be disabled.
If you want to check for permissions on runtime, you can define a new activity that takes care of requesting permissions with a built-in dialog (using ActivityCompat.requestPermissions), register this activity in the manifest, and call it from the FXActivity within a new intent that passes a list with the required permissions.
You just need to call FXActivity.getInstance().setOnActivityResultHandler() to listen to the end of that activity and resume the call if permissions were granted.
The following code is partly based in the PermissionHelper class.
I'll use the simple case of the Dialer service from the new Charm Down 3.0.0 library, that requires Manifest.permission.CALL_PHONE.
AndroidDialerService.java, under Android package
public class AndroidDialerAskService implements DialerAskService {
public static final String KEY_PERMISSIONS = "permissions";
public static final String KEY_GRANT_RESULTS = "grantResults";
public static final String KEY_REQUEST_CODE = "requestCode";
#Override
public void call(String number) {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(FXActivity.getInstance(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
FXActivity.getInstance().setOnActivityResultHandler((requestCode, resultCode, data) -> {
if (requestCode == 11112) {
// if now we have permission, resume call
if (ContextCompat.checkSelfPermission(FXActivity.getInstance(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
call(number);
}
}
});
Intent permIntent = new Intent(FXActivity.getInstance(), PermissionRequestActivity.class);
permIntent.putExtra(KEY_PERMISSIONS, new String[]{Manifest.permission.CALL_PHONE});
permIntent.putExtra(KEY_REQUEST_CODE, 11111);
FXActivity.getInstance().startActivityForResult(permIntent, 11112);
return;
}
}
if (number != null && !number.isEmpty()) {
Uri uriNumber = Uri.parse("tel:" + number);
Intent dial = new Intent(Intent.ACTION_CALL, uriNumber);
FXActivity.getInstance().startActivity(dial);
}
}
public static class PermissionRequestActivity extends Activity {
private String[] permissions;
private int requestCode;
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
FXActivity.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
finish();
}
#Override
protected void onStart() {
super.onStart();
permissions = this.getIntent().getStringArrayExtra(KEY_PERMISSIONS);
requestCode = this.getIntent().getIntExtra(KEY_REQUEST_CODE, 0);
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
}
}
AndroidManifest.xml
. . .
<uses-permission android:name="android.permission.CALL_PHONE"/>
. . .
<activity android:name="javafxports.android.FXActivity" .../>
<activity android:name="com.gluonhq.charm.down.plugins.android.AndroidDialerService$PermissionRequestActivity" />
. . .
hello i used code below and i run the project in android 3.0 tablet emulator in android application nad i get path /mnt/sdcard/ but not get fullpath.how solve it ?please help me!!And my code below
![package com.hope.project;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
WebView myWebView;
TextView mDisplay;
AsyncTask<Void, Void, Void> mRegisterTask;
String name;
String Message;
String deviceId;
String regId;
IntentFilter gcmFilter;
SharedPreferences sharedPref;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView1);
final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(
this);
myWebView.addJavascriptInterface(myJavaScriptInterface,
"AndroidFunction");
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
settings.setAllowFileAccess(true);
settings.setBuiltInZoomControls(true);
settings.setUseWideViewPort(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLoadWithOverviewMode(true);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// handle stuff here
// e.g. view.loadUrl(url);
Log.v("log", " on ovverRide " + url);
return true;
}
public void onPageFinished(WebView view, String url) {
// dismiss the indeterminate progress dialog
Log.v("log", "onPageFinished: " + url);
myWebView.setEnabled(false);
}
});
myWebView.loadUrl("file:///android_asset/www/index.html");
/* File urlName= Environment.getExternalStorageDirectory().getAbsoluteFile();
Log.v("log_tag", "urlNameDownload "+urlName);*/
/* File file\[\] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) {
String uri=f.getPath().substring(f.getPath().lastIndexOf("/") + 1);
Log.v("Name", uri);
Log.v("Name", f.getPath()+ "");
Log.v("Name", f.getAbsolutePath()+ "");
}
}*/
File dir = new File("mnt/sdcard/");
File\[\] files = (new File("mnt/sdcard/")).listFiles();
// This filter only returns directories
FileFilter dirFilter = new FileFilter() {
public boolean accept(File dir) {
return dir.isDirectory();
}
};
files = dir.listFiles(dirFilter);
for (int i=0; i<files.length; i++) {
if(files\[i\].getAbsolutePath().contains("Download"))
Log.v("log_tag","directory path : " + files\[i\].getAbsolutePath().substring(files\[i\].getAbsolutePath().lastIndexOf("/") +1));
}
}
protected void onDestroy() {
super.onDestroy();
}
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
public void DownloadUrl(String url) {
Log.v("log", "login main url " + url);
String file_url = url;
new DownloadFileFromURL().execute(file_url);
/*
* String url_new = "http://"+url; Intent i = new
* Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url_new));
* startActivity(i);
*/
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
// showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
#Override
protected String doInBackground(String... f_url) {
int count;
Log.v("log", "login main url\[0\] " + f_url\[0\]);
try {
URL url = new URL(f_url\[0\]);
name = f_url\[0\].substring(f_url\[0\].lastIndexOf("/") + 1);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream to write file
// OutputStream output = new
// FileOutputStream("/sdcard/downloadedfile.jpg");
OutputStream output = new FileOutputStream(
Environment.getExternalStorageDirectory() + "/Download/" + name);
// OutputStream output = new
// FileOutputStream("/sdcard/downloadedUrl.mp4");
byte data\[\] = new byte\[1024\];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
// pDialog.setProgress(Integer.parseInt(progress\[0\]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
// dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
/*
* Log.v("log","login main url\[0\] " +
* Environment.getExternalStorageDirectory().toString()); String
* videoPath = Environment.getExternalStorageDirectory() +"/"+name;
* Intent i = new Intent(MainActivity.this,
* VideoPlayActivity.class); i.putExtra("videoPath", videoPath);
* startActivity(i);
*/
Toast.makeText(MainActivity.this, "DownLoad Is Completed",
Toast.LENGTH_LONG).show();
}
}
}
Instead of hardcoding mnt/sdcard/ you should use the Environment object.
Specifically:
File dir = Environment.getExternalStorageDirectory();
Will give you a file object that is automatically pointing in the proper place for the External Storage of the device that it is running on.
Also, you've posted your entire Activity. The vast majority of it is unrelated to the problem you are having. In the future it is more likely that you'll get good help on StackOverflow if you take out a smaller section of your code that specifically relates to the problem you are having. It makes it easier for people who are answering to figure out your situation.
I am downloading files from server using the DownloadManager class as mentioned here. The image downloaded can be accessed from the Downloads folder on android. When I click the downloaded image from the Downloads, the image is displayed. But if it try to downlaod an apk instead of an image, the downloaded apk cannot be installed from the Downloads folder, a problem parsing the package error appears. Why?
Here is what I am doing (the same as mentioned in the link above):
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class DownloadManagerActivity extends Activity {
private long enqueue;
private DownloadManager dm;
/** Called when the activity is first created. */
#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)) {
/* ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));*/
}
}
}
}
};
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://xxx.xxx.x.xxx/MyApp.apk"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
}
How can I download files using Android downloader? (The downloader that WebBrowser is using that too).
I tried something like this :
Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse("MyUrl"));
startActivity(i);
Any better way?
Edit
I am using Android 2.2
Here you go.
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class DownloadManagerActivity extends Activity {
private long enqueue;
private DownloadManager dm;
/** Called when the activity is first created. */
#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)) {
ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("url for file to download"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
}
Don't forget to add android.permission.internet in manifest.
If you want to download it onto the user's SD card from an URL, you can just open it in the phone's default internet browser.
String url = "https://appharbor.com/assets/images/stackoverflow-logo.png";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
(Code from this question)
The android browser will then start and the file (in this case an image) will be downloaded.
You need to use HttpUrlConnection to do this on Android 2.2 and below. There is a very detailed tutorial on the internet that shows you how to do this (with a progress dialog box too).
Remember you must use an AsyncTask or a Thread to ensure that you do not block the UI thread during the actual download!
Yes, you cant perform network operation in the main thread. You can look up to this repository to download file.