我无法通过WebView下载媒体文件,无论是音频还是视频。我一直试图解决这个问题超过三天我已经在互联网上阅读了与此问题有关的威胁,但我并没有&#39似乎找到了解决方案。如果有人可以帮助我,我真的很感激。这对我来说意义重大。
MainActivity.java
public class MainActivity extends ActionBarActivity implements ViewAnimator.ViewAnimatorListener {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private List<SlideMenuItem> list = new ArrayList<>();
private ContentFragment contentFragment;
private ViewAnimator viewAnimator;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentFragment = ContentFragment.newInstance(R.drawable.content_music);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, contentFragment)
.commit();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setScrimColor(Color.TRANSPARENT);
linearLayout = (LinearLayout) findViewById(R.id.left_drawer);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
}
});
setActionBar();
createMenuList();
viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this);
}
private void setActionBar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
toolbar, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
linearLayout.removeAllViews();
linearLayout.invalidate();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset > 0.6 && linearLayout.getChildCount() == 0)
viewAnimator.showMenuContent();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
Log.e(getClass().getSimpleName(),
"onMenuOpened...unable to set icons for overflow menu",
e);
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.profile) {
Intent intent=new Intent(MainActivity.this,AboutUsActivity.class);
startActivity(intent);
}
if(id==R.id.Logout){
Intent intent=new Intent(MainActivity.this,SettingActivity.class);
startActivity(intent);
}
if(id==R.id.Settings) {
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
startActivity(intent);
}
if(id==R.id.About_bridgeGap){
Intent intent=new Intent(MainActivity.this,AboutUsActivity.class);
startActivity(intent);
}
return true;
}
@Override
public ScreenShotable onSwitch(Resourceble resourceble, ScreenShotable screenShotable, int position) {
int primaryColorCanvas = R.color.colorPrimary;
switch (resourceble.getName()) {
case ContentFragment.CLOSE:
return screenShotable;
case ContentFragment.HOME:
ContentFragment fragment = ContentFragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment)
.addToBackStack("Home").commit();
return replaceFragment(fragment, position);
case Artists_Fragment.ARTISTS:
Artists_Fragment fragment2 = Artists_Fragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment2)
.addToBackStack("Artists").commit();
return replaceFragment(fragment2, position);
case AudioFragment.AUDIO:
AudioFragment fragment3 = AudioFragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment3)
.addToBackStack("Audio").commit();
return replaceFragment(fragment3, position);
case VideoFragment.VIDEO:
VideoFragment fragment4 = VideoFragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment4)
.addToBackStack("Video").commit();
return replaceFragment(fragment4, position);
case InterviewsFragment.INTERVIEWS:
InterviewsFragment fragment5 = InterviewsFragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment5)
.addToBackStack("Interviews").commit();
return replaceFragment(fragment5, position);
case BlogFragment.BLOG:
BlogFragment fragment6 = BlogFragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment6)
.addToBackStack("Blog").commit();
return replaceFragment(fragment6, position);
case ContactFragment.CONTACT:
ContactFragment fragment7 = ContactFragment.newInstance(primaryColorCanvas);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment7)
.addToBackStack("Contact").commit();
default:
return replaceFragment(screenShotable, position);
}
}
private ScreenShotable replaceFragment(ScreenShotable screenShotable, int position) {
View view = findViewById(R.id.content_frame);
int finalRadius = Math.max(view.getWidth(), view.getHeight());
SupportAnimator animator = ViewAnimationUtils.createCircularReveal(view, 0, position, 0, finalRadius);
animator.setInterpolator(new AccelerateInterpolator());
animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION);
findViewById(R.id.content_overlay).setBackground(new BitmapDrawable(getResources(), screenShotable.getBitmap()));
animator.start();
return screenShotable;
}
private void createMenuList() {
SlideMenuItem menuItem0 = new SlideMenuItem(contentFragment.CLOSE, R.drawable.icn_close);
list.add(menuItem0);
SlideMenuItem menuItem1 = new SlideMenuItem(contentFragment.HOME, R.drawable.home);
list.add(menuItem1);
SlideMenuItem menuIte2 = new SlideMenuItem(Artists_Fragment.ARTISTS, R.drawable.singer);
list.add(menuIte2);
SlideMenuItem menuItem3 = new SlideMenuItem(AudioFragment.AUDIO, R.drawable.music);
list.add(menuItem3);
SlideMenuItem menuItem4 = new SlideMenuItem(VideoFragment.VIDEO, R.drawable.video);
list.add(menuItem4);
SlideMenuItem menuItem5 = new SlideMenuItem(InterviewsFragment.INTERVIEWS, R.drawable.interviews);
list.add(menuItem5);
SlideMenuItem menuItem6 = new SlideMenuItem(BlogFragment.BLOG, R.drawable.blogblue);
list.add(menuItem6);
SlideMenuItem menuItem7 = new SlideMenuItem(ContactFragment.CONTACT, R.drawable.contact_us_logos);
list.add(menuItem7);
}
@Override
public void disableHomeButton() {
getSupportActionBar().setHomeButtonEnabled(false);
}
@Override
public void enableHomeButton() {
getSupportActionBar().setHomeButtonEnabled(true);
drawerLayout.closeDrawers();
}
@Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
@Override
public void addViewToContainer(View view) {
linearLayout.addView(view);
}
}
Artists_Fragment.java
public class Artists_Fragment extends Fragment implements ScreenShotable {
private View containerView;
private WebView mwebView;
protected int res;
private Bitmap bitmap;
public ProgressBar progressBar;
public static final String ARTISTS = "Artists";
public static Artists_Fragment newInstance(int resId) {
Artists_Fragment contentFragment = new Artists_Fragment();
Bundle bundle = new Bundle();
bundle.putInt(Integer.class.getName(), resId);
contentFragment.setArguments(bundle);
return contentFragment;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.containerView = view.findViewById(R.id.container);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
res = getArguments().getInt(Integer.class.getName());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_artists, container, false);
mwebView = (WebView) rootView.findViewById(R.id.webview);
mwebView.loadUrl("http://xclusiveinterviews.com/artists/");
WebSettings webSettings = mwebView.getSettings();
mwebView.getSettings().setUserAgentString("BridgeGap");
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setJavaScriptEnabled(true);
mwebView.setWebViewClient(new myWebViewClient());
mwebView.setWebChromeClient(new myWebChromeClient());
progressBar = (ProgressBar) rootView.findViewById(R.id.progressbar);
/** Handle back press for WebView **/
mwebView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if((i==keyEvent.KEYCODE_BACK)&&mwebView.canGoBack());
mwebView.goBack();
return true;
}
});
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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getActivity().getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
return rootView;
}
private class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("xclusiveinterviews.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Intent intent = new Intent(getActivity(), ErrorActivity.class);
startActivity(intent);
}
}
private class myWebChromeClient extends WebChromeClient{
}
@Override
public void takeScreenShot() {
Thread thread = new Thread() {
@Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(containerView.getWidth(),
containerView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
containerView.draw(canvas);
Artists_Fragment.this.bitmap = bitmap;
}
};
thread.start();
}
@Override
public Bitmap getBitmap() {
return bitmap;
}
}
logcat的
07-11 16:44:02.135 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onProgressChanged=0
07-11 16:44:02.137 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onPageStart, url = http://xclusiveinterviews.com
/wp-admin/admin-post.php
07-11 16:44:02.137 6393-6456/com.bridgegap.akang.bridgegap
D/WebViewCallback:
shouldInterceptRequest=http://xclusiveinterviews.com/wp-admin/admin-
post.php
07-11 16:44:02.144 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onLoadResource=http://xclusiveinterviews.com/wp-
admin/admin-post.php
07-11 16:44:02.144 6393-6489/com.bridgegap.akang.bridgegap D/libc-
netbsd: getaddrinfo: xclusiveinterviews.com get result from proxy >>
07-11 16:44:02.169 6393-6393/com.bridgegap.akang.bridgegap
D/AwContents: setContentsSize 324, 1059
07-11 16:44:02.170 6393-6393/com.bridgegap.akang.bridgegap
D/AwContents: setPageScaleFactor 1.0
07-11 16:44:04.680 6393-6456/com.bridgegap.akang.bridgegap W/chromium:
[WARNING:aw_network_delegate.cc(66)] http://xclusiveinterviews.com/wp-
admin/admin-post.php#-3#1
07-11 16:44:04.685 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onProgressChanged=100
07-11 16:44:04.702 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onPageFinished, url = http://xclusiveinterviews.com
/product/breathe-in-a-new-life/
07-11 16:44:02.135 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onProgressChanged=0
07-11 16:44:02.137 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onPageStart, url = http://xclusiveinterviews.com/wp-
admin/admin-post.php
07-11 16:44:02.137 6393-6456/com.bridgegap.akang.bridgegap
D/WebViewCallback: shouldInterceptRequest=http://xclusiveinterviews.com
/wp-admin/admin-post.php
07-11 16:44:02.144 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onLoadResource=http://xclusiveinterviews.com/wp-
admin/admin-post.php
07-11 16:44:02.144 6393-6489/com.bridgegap.akang.bridgegap D/libc-
netbsd: getaddrinfo: xclusiveinterviews.com get result from proxy >>
07-11 16:44:02.169 6393-6393/com.bridgegap.akang.bridgegap
D/AwContents: setContentsSize 324, 1059
07-11 16:44:02.170 6393-6393/com.bridgegap.akang.bridgegap D/AwContents:
setPageScaleFactor 1.0
07-11 16:44:04.680 6393-6456/com.bridgegap.akang.bridgegap W/chromium:
[WARNING:aw_network_delegate.cc(66)] http://xclusiveinterviews.com/wp-
admin/admin-post.php#-3#1
07-11 16:44:04.685 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onProgressChanged=100
07-11 16:44:04.702 6393-6393/com.bridgegap.akang.bridgegap
D/WebViewCallback: onPageFinished, url = http://xclusiveinterviews.com
/product/breathe-in-a-new-life/
答案 0 :(得分:0)
public class Artists_Fragment extends Fragment实现ScreenShotable {
private View containerView;
private WebView mwebView;
protected int res;
private Bitmap bitmap;
public ProgressBar progressBar;
public static final String ARTISTS = "Artists";
public static Artists_Fragment newInstance(int resId) {
Artists_Fragment contentFragment = new Artists_Fragment();
Bundle bundle = new Bundle();
bundle.putInt(Integer.class.getName(), resId);
contentFragment.setArguments(bundle);
return contentFragment;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.containerView = view.findViewById(R.id.container);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
res = getArguments().getInt(Integer.class.getName());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_artists, container, false);
mwebView = (WebView) rootView.findViewById(R.id.webview);
mwebView.loadUrl("http://xclusiveinterviews.com/artists/");
WebSettings webSettings = mwebView.getSettings();
mwebView.getSettings().setUserAgentString("BridgeGap");
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setJavaScriptEnabled(true);
mwebView.setWebViewClient(new myWebViewClient());
mwebView.setWebChromeClient(new myWebChromeClient());
progressBar = (ProgressBar) rootView.findViewById(R.id.progressbar);
/** Handle back press for WebView **/
mwebView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((i == keyEvent.KEYCODE_BACK) && mwebView.canGoBack()) ;
mwebView.goBack();
return true;
}
});
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(mimeType);
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,
mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getActivity().getApplicationContext(), "Downloading File",
Toast.LENGTH_LONG).show();
}
});
return rootView;
}
private class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("xclusiveinterviews.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Intent intent = new Intent(getActivity(), ErrorActivity.class);
startActivity(intent);
}
}
private class myWebChromeClient extends WebChromeClient{
}
@Override
public void takeScreenShot() {
Thread thread = new Thread() {
@Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(containerView.getWidth(),
containerView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
containerView.draw(canvas);
Artists_Fragment.this.bitmap = bitmap;
}
};
thread.start();
}
@Override
public Bitmap getBitmap() {
return bitmap;
}
}