@@ -8,13 +8,15 @@ import android.content.SharedPreferences
8
8
import android.database.Cursor
9
9
import android.graphics.Bitmap
10
10
import android.graphics.BitmapFactory
11
+ import android.media.MediaScannerConnection
11
12
import android.net.Uri
12
13
import android.provider.MediaStore
13
14
import android.support.customtabs.CustomTabsIntent
14
15
import android.support.design.widget.AppBarLayout
15
16
import android.support.v4.app.Fragment
16
17
import android.support.v4.content.ContextCompat
17
18
import android.support.v7.app.AlertDialog
19
+ import android.util.Log
18
20
import android.view.LayoutInflater
19
21
import android.view.View
20
22
import android.webkit.JavascriptInterface
@@ -24,15 +26,19 @@ import android.widget.Button
24
26
import android.widget.ImageView
25
27
import android.widget.RadioGroup
26
28
import org.jetbrains.anko.longToast
29
+ import org.jetbrains.anko.toast
27
30
import tech.lincaiqi.PhotoTimeFix.R
28
31
import java.io.File
32
+ import java.text.SimpleDateFormat
29
33
import java.util.*
34
+ import java.util.regex.Pattern
30
35
31
36
class CoreK (private var context : Context ) {
32
37
33
- fun initFragment (preferences : SharedPreferences , editor : SharedPreferences .Editor , chooseBtn : Button , radioGroup : RadioGroup , fragment : Fragment ) {
38
+ fun initFragment (preferences : SharedPreferences , editor : SharedPreferences .Editor , chooseBtn : Button , radioGroup : RadioGroup , fragment : Fragment ) {
34
39
radioGroup.check(preferences.getInt(" mode" , R .id.radioButton))
35
40
chooseBtn.setOnClickListener {
41
+ context.longToast(" 由于系统限制(其实是我懒),请选择文件夹内任意一张图片" )
36
42
chooseFile(fragment)
37
43
}
38
44
radioGroup.setOnCheckedChangeListener { _, i ->
@@ -46,11 +52,11 @@ class CoreK(private var context: Context) {
46
52
val builder = AlertDialog .Builder (context)
47
53
try {
48
54
val view = LayoutInflater .from(context).inflate(R .layout.about, null )
49
- val webview : WebView = view.findViewById(R .id.webview)
50
- webview .webViewClient = WebViewClient ()
51
- webview .loadUrl(" file:///android_asset/about.html" )
52
- webview .settings.javaScriptEnabled = true
53
- webview .addJavascriptInterface(this , " openGit" )
55
+ val webView : WebView = view.findViewById(R .id.webview)
56
+ webView .webViewClient = WebViewClient ()
57
+ webView .loadUrl(" file:///android_asset/about.html" )
58
+ webView .settings.javaScriptEnabled = true
59
+ webView .addJavascriptInterface(this , " openGit" )
54
60
builder.setView(view)
55
61
} catch (e: Exception ) {
56
62
builder.setMessage(" 加载Webview错误,已停止显示帮助窗口。\n 该错误并不影响正常功能运行,且开发者仅在模拟器上遇到过,如果出现此对话框请与开发者联系。" )
@@ -88,13 +94,13 @@ class CoreK(private var context: Context) {
88
94
89
95
}
90
96
91
- private fun chooseFile (fragment : Fragment ) {
97
+ private fun chooseFile (fragment : Fragment ) {
92
98
val intent = Intent (Intent .ACTION_PICK , null )
93
99
intent.setDataAndType(MediaStore .Images .Media .EXTERNAL_CONTENT_URI , " image/*" )
94
- fragment.startActivityForResult(intent,0 )
100
+ fragment.startActivityForResult(intent, 0 )
95
101
}
96
102
97
- fun resultSolve (requestCode : Int , data : Intent ? ) : String {
103
+ fun resultSolve (requestCode : Int , data : Intent ? ): String {
98
104
try {
99
105
return if (requestCode == 0 ) {
100
106
val originalUri = data!! .data
@@ -110,41 +116,79 @@ class CoreK(private var context: Context) {
110
116
cursor.close()
111
117
path
112
118
} else " error"
113
- }else " error"
119
+ } else " error"
114
120
115
- } catch (e : java.lang.Exception ) {
121
+ } catch (e: java.lang.Exception ) {
116
122
e.printStackTrace()
117
123
return " error"
118
124
}
119
125
}
120
126
121
- fun updateDate (path : String , activity : Activity ) : String {
127
+ fun updateDate (path : String , activity : Activity ): Array < String ?> {
122
128
val file = File (path)
123
- val imageView : ImageView = activity.findViewById(R .id.user_bg)
124
- return if (file.exists()&& file.isFile) {
125
- val bm : Bitmap = BitmapFactory .decodeFile(path)
129
+ val imageView: ImageView = activity.findViewById(R .id.user_bg)
130
+ val returnArray = arrayOfNulls<String >(2 )
131
+ if (file.exists() && file.isFile) {
132
+ val bm: Bitmap = BitmapFactory .decodeFile(path)
126
133
imageView.setImageBitmap(bm)
127
- updateAppbar(activity,false )
128
- Date (file.lastModified()).toString()
129
- }
130
- else {
134
+ updateAppbar(activity, false )
135
+ val sdf = SimpleDateFormat (" yyyy-MM-dd HH:mm:ss" , Locale .getDefault())
136
+ returnArray[0 ]= sdf.format(Date (file.lastModified()))
137
+ var time = file.name
138
+ time = Pattern .compile(" [^0-9]" ).matcher(time).replaceAll(" " ).trim { it <= ' ' }
139
+ Log .d(" date" ,time)
140
+ if (time.contains(" 20" ) && time.substring(time.indexOf(" 20" )).length >= 12 ) {
141
+ val targetTime = time.substring(time.indexOf(" 20" ), time.indexOf(" 20" ) + 12 )
142
+ try {
143
+ returnArray[1 ] = sdf.format(SimpleDateFormat (" yyyyMMddHHmm" , Locale .getDefault()).parse(targetTime))
144
+ } catch (e : Exception ) {
145
+ returnArray[1 ] = " "
146
+ e.printStackTrace()
147
+ }
148
+ } else {
149
+ returnArray[1 ] = " "
150
+ }
151
+ } else {
131
152
imageView.setImageBitmap(null )
132
- updateAppbar(activity,true )
133
- " "
153
+ updateAppbar(activity, true )
154
+ returnArray[0 ] = " "
155
+ returnArray[1 ] = " "
134
156
}
157
+ return returnArray
135
158
}
136
159
137
- fun updateAppbar (activity : Activity , scrollAble : Boolean ) {
160
+ fun updateAppbar (activity : Activity , scrollAble : Boolean ) {
138
161
val mAppBarLayout = activity.findViewById<AppBarLayout >(R .id.app_bars)
139
162
val mAppBarChildAt: View = mAppBarLayout.getChildAt(0 )
140
163
val mAppBarParams: AppBarLayout .LayoutParams = mAppBarChildAt.layoutParams as AppBarLayout .LayoutParams
141
164
mAppBarParams.scrollFlags = if (scrollAble) AppBarLayout .LayoutParams .SCROLL_FLAG_SCROLL or AppBarLayout .LayoutParams .SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
142
- else 0
165
+ else 0
143
166
mAppBarChildAt.layoutParams = mAppBarParams
144
167
/* 作者:Silas_
145
168
来源:CSDN
146
169
原文:https://blog.csdn.net/qq_31852701/article/details/80859644
147
170
版权声明:本文为博主原创文章,转载请附上博文链接! */
148
171
}
149
172
173
+ fun freshMedia (path : String , context : Context ) {
174
+ val file = File (path)
175
+ val paths : Array <String ?>
176
+ if (! file.exists()) {
177
+ context.toast(" 文件或目录不存在" )
178
+ return
179
+ }
180
+ if (file.isFile) {
181
+ paths = arrayOf(path)
182
+ } else {
183
+ val files = file.listFiles()
184
+ paths = arrayOfNulls(files.size)
185
+ for ((i, f) in files.withIndex()) {
186
+ paths[i] = f.absolutePath
187
+ }
188
+ }
189
+ Log .d(" path" ,paths.toString())
190
+ MediaScannerConnection .scanFile(context, paths, null ) { _, _ -> }
191
+ context.toast(" 完成" )
192
+ }
193
+
150
194
}
0 commit comments