优化下载对话框百度网页图片文件名,支持修改下载文件名

This commit is contained in:
sonichy 2020-09-20 12:13:05 +08:00
parent 7f19736b56
commit b2be5a9ddf
6 changed files with 37 additions and 122 deletions

BIN
app.apk

Binary file not shown.

View File

@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hty.browser"
android:versionCode="4"
android:versionName="4.13">
android:versionName="4.14">
<application
android:icon="@drawable/ic_launcher"

View File

@ -9,7 +9,7 @@ p { text-indent:2em; }
</style>
</head>
<body>
<h2 align=center><img src=logo.png><br>海天鹰浏览器 V4.13</h2>
<h2 align=center><img src=logo.png><br>海天鹰浏览器 V4.14</h2>
<p>内置扩展的浏览器。</p>
<p>扩展:链接关键字过滤,链接关键字高亮,图片自定义过滤,视频独立播放。</p>
<p>作者:海天鹰</p>
@ -23,6 +23,7 @@ p { text-indent:2em; }
<p><a href="https://stackoverflow.com/questions/3462582/display-the-android-webviews-favicon" target="_blank">获取网页图标</a></p>
<p><a href="https://www.jianshu.com/p/c9a18050a249" target="_blank">字符串转Bitmap</a></p>
<h3>更新日志:</h3>
<h3>V4.14 (2020-09-20)</h3><ol><li>优化下载对话框百度网页图片文件名填充,支持修改下载文件名。</li></ol>
<h3>V4.13 (2020-08-30)</h3><ol><li>图片查看改为在新窗口中打开。</li><li>修复修改、删除收藏不能记忆位置问题。</li></ol>
<h3>V4.12 (2020-07-17)</h3><ol><li>适配全面屏导航栏背景色。</li><li>收藏夹无标题栏自定义菜单改成标题栏菜单。</li></ol>
<h3>V4.11 (2020-03-03)</h3><ol><li>收藏夹增加数目统计。</li><li>收藏夹增加菜单导出HTML、CSV。</li></ol>

View File

@ -14,6 +14,7 @@ import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
@ -76,7 +77,6 @@ import android.webkit.ConsoleMessage;
import android.webkit.DownloadListener;
import android.webkit.GeolocationPermissions;
import android.webkit.JsResult;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.CustomViewCallback;
@ -379,10 +379,9 @@ public class MainActivity extends Activity {
private class MyWebViewDownLoadListener implements DownloadListener {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.e(Thread.currentThread().getStackTrace()[2] + ": ", url);
//downloadBySystem(url, "", "");
dialog_new_download(url, "", "");
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
Log.e(Thread.currentThread().getStackTrace()[2] + ": ", url + ", " + userAgent + "," + contentDisposition + ", " + mimeType + ", " + contentLength);
dialog_download(url, contentDisposition, mimeType, contentLength);
}
}
@ -392,10 +391,10 @@ public class MainActivity extends Activity {
WebView w = (WebView) v;
HitTestResult result = w.getHitTestResult();
HTRE = result.getExtra();
menu.setHeaderIcon(android.R.drawable.ic_menu_report_image);
Log.e(Thread.currentThread().getStackTrace()[2] + ": ", HTRE);
menu.setHeaderTitle(HTRE);
if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
menu.setHeaderIcon(android.R.drawable.ic_menu_gallery); // Context menu items do not support icons
menu.setHeaderIcon(android.R.drawable.ic_menu_gallery);
menu.add(0, 0, 0, "查看图片");
menu.add(0, 1, 1, "复制图片");
menu.add(0, 2, 2, "保存图片").setIcon(android.R.drawable.ic_menu_save); // Context menu items do not support icons
@ -427,7 +426,10 @@ public class MainActivity extends Activity {
clipboardManager.setPrimaryClip(clipData);
break;
case 2:
dialog_new_download(HTRE, "", "");
String mime = URLConnection.getFileNameMap().getContentTypeFor(HTRE);
Log.e(Thread.currentThread().getStackTrace()[2] + ": ", "" + mime);
if (mime == null) mime = "";
dialog_download(HTRE, "", mime, 0);
break;
case 3:
clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
@ -794,7 +796,7 @@ public class MainActivity extends Activity {
builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String dir = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Screenshots/";
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + File.separator + "Screenshots";
File temp = new File(dir); // 如果文件夹不存在则创建
if (!temp.exists()) {
temp.mkdir();
@ -1082,7 +1084,7 @@ public class MainActivity extends Activity {
}
// 调用系统下载https://www.jianshu.com/p/6e38e1ef203a
private void downloadBySystem(String surl, String contentDisposition, String mimeType) {
private void downloadBySystem(String surl, String contentDisposition, String mimeType, String path, String fileName) {
// 指定下载地址
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(surl));
// 允许媒体扫描根据下载的文件类型被加入相册音乐等媒体库
@ -1098,8 +1100,6 @@ public class MainActivity extends Activity {
// 允许漫游时下载
request.setAllowedOverRoaming(true);
// 设置下载文件保存的路径和文件名
String fileName = URLUtil.guessFileName(surl, contentDisposition, mimeType);
Log.e(Thread.currentThread().getStackTrace()[2] + "", fileName);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
// 自定义下载路径
//request.setDestinationUri();
@ -1107,7 +1107,7 @@ public class MainActivity extends Activity {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
// 添加一个下载任务
long downloadId = downloadManager.enqueue(request);
Log.e(Thread.currentThread().getStackTrace()[2] + "", downloadId+"");
Log.e(Thread.currentThread().getStackTrace()[2] + "", downloadId + "");
if(surl == urlUpdate){
downloadIdUpdate = downloadId;
}
@ -1137,7 +1137,7 @@ public class MainActivity extends Activity {
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadBySystem(urlUpdate, "", "");
downloadBySystem(urlUpdate, "", "", "", "HTYBrowser.apk");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@ -1462,15 +1462,22 @@ public class MainActivity extends Activity {
}
}
void dialog_new_download(final String surl, String contentDisposition, String mimeType){
View view = LayoutInflater.from(this).inflate(R.layout.dialog_new_download, null, false);
void dialog_download(final String surl, String contentDisposition, String mimeType, long contentLength){
View view = LayoutInflater.from(this).inflate(R.layout.dialog_download, null, false);
final EditText editText_download_url = (EditText) view.findViewById(R.id.editText_download_url);
editText_download_url.setText(surl);
EditText editText_download_filename = (EditText) view.findViewById(R.id.editText_download_filename);
editText_download_filename.setText(surl.substring(surl.lastIndexOf("/")+1));
final EditText editText_download_filename = (EditText) view.findViewById(R.id.editText_download_filename);
String filename = surl.substring(surl.lastIndexOf("/") + 1);
Log.e(Thread.currentThread().getStackTrace()[2] + " ", "FileName: " + filename);
if (filename.contains("&f=JPEG?")) {//百度网页图片重命名
SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String stime = SDF.format(date);
filename = stime + ".jpg";
}
editText_download_filename.setText(filename);
editText_download_path = (EditText) view.findViewById(R.id.editText_download_path);
//String path = Environment.getExternalStorageDirectory().getPath() + "/download";
String path = Environment.DIRECTORY_DOWNLOADS;
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
editText_download_path.setText(path);
ImageButton imageButton_path = (ImageButton) view.findViewById(R.id.imageButton_path);
@ -1484,10 +1491,12 @@ public class MainActivity extends Activity {
});
textView_filesize = (TextView) view.findViewById(R.id.textView_filesize);
if(!surl.startsWith("data:")) {
if (contentLength == 0) {
GetFileLengthThread getFileLengthThread = new GetFileLengthThread();
getFileLengthThread.surl = editText_download_url.getText().toString();
getFileLengthThread.start();
} else {
textView_filesize.setText(Formatter.formatFileSize(MainActivity.this, contentLength));
}
final AlertDialog dialog = new AlertDialog.Builder(this)
@ -1497,10 +1506,10 @@ public class MainActivity extends Activity {
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_SHORT).show();
String url = editText_download_url.getText().toString();
if(!url.equals("")) {
downloadBySystem(url, "", "");
if (!url.equals("")) {
downloadBySystem(url, "", URLConnection.getFileNameMap().getContentTypeFor(url), "", editText_download_filename.getText().toString());
IMM.hideSoftInputFromWindow(editText_download_filename.getWindowToken(), 0);
}
}
})

View File

@ -1,95 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:columnCount="3">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="下载地址:"/>
<EditText
android:id="@+id/editText_download_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:ems="10"
android:singleLine="true"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:text="文件名称:"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:text="保存路径:"/>
<EditText
android:id="@+id/editText_download_filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:ems="10"
android:singleLine="true"/>
<EditText
android:id="@+id/editText_download_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:ems="10"
android:singleLine="true"/>
<ImageButton
android:id="@+id/imageButton_path"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_row="2"
android:layout_gravity="center"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/folder_download"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:layout_gravity="center"
android:text="文件大小:"/>
<TextView
android:id="@+id/textView_filesize"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="3"
android:layout_column="1"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</GridLayout>
</LinearLayout>

View File

@ -1 +1 @@
4.13
4.14