mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-15 18:48:52 +01:00
Save part to disk and share using content provider
Fixes #1508 and fixes #1468
This commit is contained in:
@@ -22,14 +22,8 @@ import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Matrix
|
||||
import android.media.MediaScannerConnection
|
||||
import android.net.Uri
|
||||
import android.os.Environment
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
|
||||
class ImageRepostoryImpl @Inject constructor(private val context: Context) : ImageRepository {
|
||||
@@ -58,24 +52,4 @@ class ImageRepostoryImpl @Inject constructor(private val context: Context) : Ima
|
||||
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true)
|
||||
}
|
||||
|
||||
override fun saveImage(uri: Uri) {
|
||||
val type = context.contentResolver.getType(uri)?.split("/") ?: return
|
||||
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Media").apply { mkdirs() }
|
||||
val file = File(dir, "${type.first()}${System.currentTimeMillis()}.${type.last()}")
|
||||
|
||||
try {
|
||||
FileOutputStream(file).use { outputStream ->
|
||||
context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
inputStream.copyTo(outputStream, 1024)
|
||||
}
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
MediaScannerConnection.scanFile(context, arrayOf(file.path), null, null)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,9 +25,12 @@ import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.media.MediaScannerConnection
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.Telephony
|
||||
import android.telephony.SmsManager
|
||||
import android.webkit.MimeTypeMap
|
||||
import androidx.core.content.contentValuesOf
|
||||
import com.google.android.mms.ContentType
|
||||
import com.google.android.mms.MMSPart
|
||||
@@ -54,6 +57,10 @@ import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -132,6 +139,33 @@ class MessageRepositoryImpl @Inject constructor(
|
||||
.findAllAsync()
|
||||
}
|
||||
|
||||
override fun savePart(id: Long): File? {
|
||||
val part = getPart(id) ?: return null
|
||||
|
||||
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(part.type) ?: return null
|
||||
val date = part.messages?.first()?.date
|
||||
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Media").apply { mkdirs() }
|
||||
val fileName = part.name?.takeIf { name -> name.endsWith(extension) }
|
||||
?: "${part.type.split("/").last()}_$date.$extension"
|
||||
val file = File(dir, fileName)
|
||||
|
||||
try {
|
||||
FileOutputStream(file).use { outputStream ->
|
||||
context.contentResolver.openInputStream(part.getUri())?.use { inputStream ->
|
||||
inputStream.copyTo(outputStream, 1024)
|
||||
}
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
MediaScannerConnection.scanFile(context, arrayOf(file.path), null, null)
|
||||
|
||||
return file.takeIf { it.exists() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the list of messages which should be shown in the notification
|
||||
* for a given conversation
|
||||
|
||||
Reference in New Issue
Block a user