feat: share
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.lsp.view.common
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.pm.ResolveInfo
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import coil.ImageLoader
|
||||
import coil.request.ImageRequest
|
||||
import com.lsp.view.R
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
|
||||
fun share(url: String, context: Context) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
|
||||
val imageLoader = ImageLoader.Builder(context)
|
||||
.build()
|
||||
|
||||
val imageRequest = ImageRequest.Builder(context).data(url).allowHardware(true).build()
|
||||
val cacheImage = imageLoader.execute(imageRequest)
|
||||
val bitmap = cacheImage.drawable?.toBitmap()
|
||||
|
||||
val path = File("${context.cacheDir}/image")
|
||||
val split = url.split("/")
|
||||
val fileName = split[split.size - 1]
|
||||
if (!path.exists())
|
||||
path.mkdirs()
|
||||
|
||||
|
||||
var file = File("${context.cacheDir}/image/$fileName")
|
||||
val downloadFile =
|
||||
File("${Environment.getExternalStorageDirectory()}/${Environment.DIRECTORY_PICTURES}/LspMake/$fileName")
|
||||
|
||||
if (downloadFile.exists()) {
|
||||
file = downloadFile
|
||||
} else if (!file.exists()) {
|
||||
file.outputStream().apply {
|
||||
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, this)
|
||||
}
|
||||
}
|
||||
val imageUri = FileProvider.getUriForFile(
|
||||
context,
|
||||
"com.lsp.view.fileprovider",
|
||||
file
|
||||
|
||||
)
|
||||
context.grantUriPermission(
|
||||
context.packageName,
|
||||
imageUri,
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
)
|
||||
val shareIntent = Intent(Intent.ACTION_SEND)
|
||||
shareIntent.type = "image/*"
|
||||
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
|
||||
shareIntent.putExtra(Intent.EXTRA_TITLE, fileName)
|
||||
val intent = Intent.createChooser(shareIntent, R.string.title_share.toString())
|
||||
val resInfoList: List<ResolveInfo> = if (Build.VERSION.SDK_INT < 33) {
|
||||
context.packageManager
|
||||
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
} else {
|
||||
context.packageManager.queryIntentActivities(
|
||||
intent,
|
||||
PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
|
||||
)
|
||||
}
|
||||
for (resolveInfo in resInfoList) {
|
||||
val packageName = resolveInfo.activityInfo.packageName
|
||||
context.grantUriPermission(
|
||||
packageName,
|
||||
imageUri,
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
)
|
||||
}
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.ExperimentalMaterialApi
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
import androidx.compose.material.pullrefresh.PullRefreshIndicator
|
||||
import androidx.compose.material.pullrefresh.pullRefresh
|
||||
import androidx.compose.material.pullrefresh.rememberPullRefreshState
|
||||
@@ -86,11 +87,11 @@ import coil.compose.SubcomposeAsyncImage
|
||||
import coil.compose.SubcomposeAsyncImageContent
|
||||
import com.lsp.view.R
|
||||
import com.lsp.view.common.Config
|
||||
import com.lsp.view.common.share
|
||||
import com.lsp.view.service.DownloadService
|
||||
import com.lsp.view.ui.compose.widget.SearchBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -428,6 +429,13 @@ fun DetailScreen(navController: NavController, viewModel: PostViewModel) {
|
||||
contentDescription = "Download"
|
||||
)
|
||||
}
|
||||
IconButton(onClick = {
|
||||
uiState.selectPost?.let {
|
||||
share(it.sampleUrl,context)
|
||||
}
|
||||
}) {
|
||||
androidx.compose.material.Icon(imageVector = Icons.Default.Share, contentDescription = "Share", tint = Color.White)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user