Merge pull request #1274 from cdhiraj40/dev

Add an option to share crash report as a file after a crash
This commit is contained in:
Prathamesh More 2022-03-03 17:47:21 +05:30 committed by GitHub
commit a422fadc14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 153 additions and 0 deletions

View file

@ -2,7 +2,10 @@ package code.name.monkey.retromusic.util
import android.content.Context
import android.net.Uri
import android.util.Log
import java.io.File
import java.io.IOException
object FileUtils {
fun copyFileToUri(context: Context, fromFile: File, toUri: Uri) {
@ -13,4 +16,45 @@ object FileUtils {
}
}
}
/**
* creates a new file in storage in app specific directory.
*
* @return the file
* @throws IOException
*/
fun createFile(context: Context, directoryName: String, fileName: String, body: String, fileType: String): File {
val root = createDirectory(context, directoryName)
val filePath = "$root/$fileName$fileType"
val file = File(filePath)
// create file if not exist
if (!file.exists()) {
try {
// create a new file and write text in it.
file.createNewFile()
file.writeText(body)
Log.d(FileUtils::class.java.name, "File has been created and saved")
} catch (e: IOException) {
Log.d(FileUtils::class.java.name, e.message.toString())
}
}
return file
}
/**
* creates a new directory in storage in app specific directory.
*
* @return the file
*/
private fun createDirectory(context: Context, directoryName: String): File {
val file = File(
context.getExternalFilesDir(directoryName)
.toString()
)
if (!file.exists()) {
file.mkdir()
}
return file
}
}

View file

@ -18,6 +18,8 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.app.ActivityCompat
import androidx.core.content.FileProvider
import java.io.File
/**
* Created by hemanths on 2020-02-02.
@ -30,4 +32,16 @@ object Share {
feedIntent.putExtra(Intent.EXTRA_STREAM, uri)
ActivityCompat.startActivity(context, feedIntent, null)
}
fun shareFile(context: Context, file: File) {
val attachmentUri = FileProvider.getUriForFile(
context,
context.applicationContext.packageName,
file
)
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/*"
sharingIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri)
context.startActivity(Intent.createChooser(sharingIntent, "send bug report"))
}
}