android webview href,Android Webview not triggering a href download fil

I have added the Download Manager , but it is not triggering the download event , can any body help . When user goes to webview download option and click a link to download a file nothing happens. I have gone through many of the post in stackoverflow but nothing works for me.

public class MainActivity extends AppCompatActivity {

private WebView mWebView;

// boolean thread_running = true;

private static final String TAG = "MainActivity";

// flag for Internet connection status

Boolean isInternetPresent = false;

// Connection detector class

ConnectionDetector cd;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mWebView = (WebView) findViewById(R.id.webView);

mWebView.setWebViewClient(new MyAppWebViewClient(this));

WebSettings webSettings = mWebView.getSettings();

webSettings.setJavaScriptEnabled(true);

webSettings.setSupportZoom(false);

webSettings.setDomStorageEnabled(true);

// creating connection detector class instance

cd = new ConnectionDetector(getApplicationContext());

/**

* Check Internet status button click event

* */

isInternetPresent = cd.isConnectingToInternet();

// check for Internet status

if (isInternetPresent) {

// Internet Connection is Present

// make HTTP requests

mWebView.loadUrl("http://url");

} else {

// Internet connection is not present

// Ask user to connect to Internet

//showAlertDialog(MainActivity.this, "No Internet Connection",

//"You don't have internet connection.", false);

Toast.makeText(this, "No Internet connection, Please SwitchOn Connection", Toast.LENGTH_LONG).show();

finish();

}

}

@Override

public void onBackPressed() {

if (mWebView.canGoBack()) {

mWebView.goBack();

} else {

super.onBackPressed();

}

}

public class MyAppWebViewClient extends WebViewClient {

private Context context;

public MyAppWebViewClient(Context context) {

this.context = context;

}

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (!cd.isConnectingToInternet()) {

//internet is not present

Toast.makeText(getApplicationContext(), "Internet Stopped Working", Toast.LENGTH_SHORT).show();

finish();

}

if(url.contains(".JPG")){

DownloadManager mdDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

DownloadManager.Request request = new DownloadManager.Request(

Uri.parse(url));

File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url));

request.setDescription("Downloading ...");

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDestinationUri(Uri.fromFile(destinationFile));

mdDownloadManager.enqueue(request);

return true;

}

view.loadUrl(url);

return true;

}

public String getFileName(String url) {

String filenameWithoutExtension = "";

filenameWithoutExtension = String.valueOf(System.currentTimeMillis()

+ ".jpg");

return filenameWithoutExtension;

}

}

}

Can someone help me out here? The page loads without issue but the link to a image file in the HTML page is not working...

I am getting this following error when click on link

Writing exception to parcel

java.lang.SecurityException: No permission to write to /storage/emulated/0/1499148267411.jpg: Neither user 10109 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.

at android.app.ContextImpl.enforce(ContextImpl.java:1438)

at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1470)

at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:724)

at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:558)

at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)

at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)

at android.os.Binder.execTransact(Binder.java:453)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值