Skip to content

Commit

Permalink
修复文件分享
Browse files Browse the repository at this point in the history
  • Loading branch information
adk23333 committed Jul 27, 2023
1 parent 74a966f commit cf08855
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>

<application
android:name=".NovelApplication"
Expand All @@ -16,7 +18,7 @@
tools:targetApi="31">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="akabc.crawler.novel.MainActivity"
android:authorities="akabc.crawler.novel.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
Expand Down
21 changes: 19 additions & 2 deletions app/src/main/java/akabc/crawler/novel/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import akabc.crawler.novel.ui.page.record.HistoryScreen
import akabc.crawler.novel.ui.page.record.MarkScreen
import akabc.crawler.novel.ui.page.sfacg.OptionSF
import akabc.crawler.novel.ui.theme.Novel_CrawlerTheme
import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -22,6 +24,15 @@ class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
this.requestPermissions(
arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
), 1
)
}

setContent {
Novel_CrawlerTheme {
MainNavHost()
Expand Down Expand Up @@ -61,10 +72,16 @@ fun MainNavHost(
{ navController.navigate(Crawler) }) { navController.popBackStack() }
}
composable(HISTORY) {
HistoryScreen(viewModel, { navController.popBackStack() }, { navController.navigate(Crawler) })
HistoryScreen(
viewModel,
{ navController.popBackStack() },
{ navController.navigate(Crawler) })
}
composable(MARK) {
MarkScreen(viewModel, { navController.popBackStack() },{ navController.navigate(Crawler) })
MarkScreen(
viewModel,
{ navController.popBackStack() },
{ navController.navigate(Crawler) })
}
composable(Crawler) {
CrawlerScreen(
Expand Down
26 changes: 13 additions & 13 deletions app/src/main/java/akabc/crawler/novel/share/Share.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ import java.io.File

const val DownloadPath = "/storage/emulated/0/Download"

fun writeToExcelAndShare(task: Task, context: Context, novels: List<DbSfNovel>){
fun writeToExcelAndShare(task: Task, context: Context, novels: List<DbSfNovel>) {
val fileName = "ID-${task.base.id}-${task.base.name}.xlsx"
val file = File(DownloadPath, fileName)
if(!file.exists()){
var file = File(DownloadPath, "/${context.packageName}")
file.mkdir()
file = File("$DownloadPath/${context.packageName}", fileName)
if (!file.exists()) {
CoroutineScope(Dispatchers.Main).launch {
Toast.makeText(context, "正在导出文件到Download文件夹", Toast.LENGTH_SHORT).show()
}
writeNovelsToExcel(novels, fileName)
writeNovelsToExcel(novels, "$DownloadPath/${context.packageName}", fileName)
}
shareExcel(context, file)
}

fun writeNovelsToExcel(novels: List<DbSfNovel>, fileName: String) {
fun writeNovelsToExcel(novels: List<DbSfNovel>, path: String, fileName: String) {
workbook {
sheet {
row {
Expand Down Expand Up @@ -71,20 +73,18 @@ fun writeNovelsToExcel(novels: List<DbSfNovel>, fileName: String) {
}
}
}
}.write("$DownloadPath/$fileName")
}.write("$path/$fileName")

}

fun shareExcel(context: Context, file: File) {
try {
val authority = context.packageName + ".MainActivity"
val authority = context.packageName + ".fileprovider"
val uri = FileProvider.getUriForFile(context, authority, file)

context.startActivity(Intent(Intent.ACTION_SEND).apply {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
setDataAndType(uri, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
})
context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND).apply {
type = "application/vnd.ms-excel"
putExtra(Intent.EXTRA_STREAM, uri)
}, "分享"))
} catch (e: Exception) {
Log.d("share.Share", e.message.toString())
CoroutineScope(Dispatchers.Main).launch {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="shareExcel" path="." />
<external-path name="akabc.crawler.novel" path="Download/" />
</paths>

0 comments on commit cf08855

Please sign in to comment.