我想长按从WebView
下载图像。该代码可以正常工作并下载图像,因为我认为它可以保存在缓存中,因为每当我尝试打开图像时,它都会消失。我想将图像永久保存在内部下载文件夹中。但是如果我设置
request.setDestinationInExternalPublicDir();
它崩溃了。 现在该怎么办?
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
final WebView.HitTestResult result = mWebView.getHitTestResult();
if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
//menu.setHeaderTitle(result.getExtra());
menu.add(0, 1, 0, "Save Image")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String DownloadImageURL = result.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) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Toast.makeText(getContext(),"Image save successfully.",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(),"Error to save image.",Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
}
LogCat
输出
BoostFramework() : mPerf = com.qualcomm.qti.Performance@fe8297a
07-18 15:42:49.964 20572-20572/com.example.aashiq.tonu D/AndroidRuntime: Shutting down VM
07-18 15:42:49.968 20572-20572/com.example.aashiq.tonu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aashiq.tonu, PID: 20572
java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/down
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:636)
at com.example.aashiq.tonu.MainActivity$1.onMenuItemClick(MainActivity.java:78)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:166)
at com.android.internal.app.AlertController$AlertParams$4.onItemClick(AlertController.java:1154)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1164)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3154)
at android.widget.AbsListView$3.run(AbsListView.java:4097)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6221)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-18 15:42:50.065 20572-20572/com.example.aashiq.tonu I/Process: Sending signal. PID: 20572 SIG: 9
答案 0 :(得分:0)
似乎是一个Permission
问题。
您是否在permission
中声明了AndroidManifest.xml
?