Issue
i am noob in webview app and and java too
i want to create a webview-app for downloading website eg-downloadgram.com website but but after loading the photo it is not starting download. pls tell me the solution.
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.INTERNET" />
my website let example -- downloadgram.com
my mainactivity.java is
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE=2;.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
WebSettings webSettings=mywebView.getSettings();
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mywebView.getSettings().setDomStorageEnabled(true);
mywebView.setWebChromeClient(new WebChromeClient());
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://downloadgram.com/");
isWriteStoragePermissionGranted();
}
public class mywebClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
@Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
public boolean isReadStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted1");
return true;
} else {
Log.v(TAG,"Permission is revoked1");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
return false;
}
}
else {
Log.v(TAG,"Permission is granted1");
return true;
}
}
public boolean isWriteStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted2");
return true;
} else {
Log.v(TAG,"Permission is revoked2");
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
return false;
}
}
else {
Log.v(TAG,"Permission is granted2");
return true;
}
}
}
why there is problem in download...?? pls give me a solution...
Solution
You need to add DownloadListener
to Webview to fetch the download link from Webview, Then pass the download link to DownloadManager
mywebView.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, "newImage.jpg");//replace newImage.jpg
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "File downloading",Toast.LENGTH_LONG).show();
}
});
Answered By - Anu Martin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.