error exposed beyond app through Intent, open pdf file - android

Through base64 string iam creating a pdf file.
This pdf file is succesfully created and stored in data/users/0/cache
On a button click i want to show this pdf file.
Function in kotlin:
fun decodeArbeidscontractString() {
var fos: FileOutputStream? = null
try {
f = File(context?.cacheDir, "newPdf_f.pdf")
f!!.createNewFile()
fos = FileOutputStream(f)
val decodedString: ByteArray = Base64.decode(pdf_string_arbeidscontract,
Base64.DEFAULT)
fos.write(decodedString) //write the base64 to the pdf
fos.flush()
fos.close()
val path: Uri = Uri.fromFile(f)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(path, "application/pdf")
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
}
}
I can see the file in the emulator but cannot open it. How can i do that

Related

Android: Writing byteArray into file

I'm trying to write a byteArray received from a server. This is my code
private fun writePdf(content: ByteArray) {
val storageDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
val file = File("${storageDir?.path}/", "${Date().time}Download.pdf")
try {
// file.writeBytes(archivo)
val os = FileOutputStream(file, false)
os.write(content)
os.flush()
os.close()
} catch (e: IOException) {
e.printStackTrace()
}
val intent = Intent(Intent.ACTION_VIEW)
val uri = FileProvider
.getUriForFile(
this,
this.packageName + ".fileprovider",
file)
intent.setDataAndType(uri, "application/pdf")
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
try{
startActivity(intent)
}catch (e: Exception){
e.printStackTrace()
Toast.makeText(this, "Error", Toast.LENGTH_LONG)
.show()
}
}
The problem is that when the pdf opens it is blank, like nothing has been written.
I've tried writing with FileOutputStream and File.writeBytes.
I've checked the byteArray (in case is corrupted or something) and it has no problems.
I've checked the length() of the file before and after writing, and it's length increases accordingly.
Thanks, any kind of help is appreciated.
I finally found the problem, and solution. Everything with the code above was okay; the problem was that the flags for the intent to visualize the PDF were wrong. Instead of:
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
It should be:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

how to load local file into webview

Im trying load this file into my webview but its not loading
file path is something like
/data/user/0/com.xyzapp.app/cache/temp_file.docx //this is getpathhh
below is my code
first activity:-
val browseintent = Intent(this, WebviewActivityNew::class.java)
browseintent.putExtra("url", "file://"+getpathhh.toString())
startActivity(browseintent)
second Activity:-
val url = intent.extras!!.getString("url")
webview_comp.getSettings().setLoadsImagesAutomatically(true);
webview_comp.getSettings().setJavaScriptEnabled(true);
webview_comp.getSettings().setAllowContentAccess(true);
webview_comp.getSettings().setAllowFileAccess(true);
webview_comp.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview_comp.getSettings().setAppCachePath(url);
webview_comp.getSettings().setAppCacheEnabled(true);
webview_comp.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
url?.let { webview_comp.loadUrl(it) };
if i use Action_view
val pdfOpenintent = Intent(Intent.ACTION_VIEW)
pdfOpenintent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
pdfOpenintent.setDataAndType(Uri.fromFile(getpathhh), "*/*")
try {
startActivity(pdfOpenintent)
} catch (e: ActivityNotFoundException) {
}
im getting crash as 'E/UncaughtException: android.os.FileUriExposedException: file:///data/user/0/com.xyzapp.app/cache/temp_file.docx exposed beyond app through Intent.getData()'
following what i have tried
val pdfOpenintent = Intent(Intent.ACTION_VIEW)
pdfOpenintent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
pdfOpenintent.setDataAndType(
FileProvider.getUriForFile(
applicationContext,
"com.xyzapp.app.provider",
getpathhh
), "*/*")
try {
startActivity(pdfOpenintent)
} catch (e: ActivityNotFoundException) {
}
/data/user/0/com.xyzapp.app/cache/temp_file.docx
A WebView is for .html documents.
It cannot display a .docx file.

How to convert bytearray to pdf and show it with ACTION_VIEW intent

I am receveving a pdf as an encoded base64 String. I would like to show the PDF with ACTION_VIEW intent. How can I do that?
What I have so far is this
val byteArray = Base64.decode(base64String, Base64.DEFAULT)
val file = FileUtils.createFile(requireContext(), "application/pdf")
val fos = FileOutputStream(file)
fos.write(byteArray)
fos.close()
val uri = FileProvider.getUriForFile(requireContext(), requireActivity().getString(R.string.file_provider), file)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/pdf")
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent)
createFile function looks like
fun createFile(context: Context, mimeType: String): File {
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
val fileName = "TMP_" + timeStamp + "_"
val suffix = "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) //.pdf
return context.getExternalFilesDir("Documents")?.let {
if (!it.exists()) {
it.mkdir()
}
File.createTempFile(fileName, suffix, it)
} ?: File.createTempFile(fileName, suffix, Environment.getExternalStorageDirectory())
}
The intent starts properly but when I try to open it with a pdf viewer it says the file is corrupted.
I simple changed this
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
to
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
works fine now!

how to open pdf file from internal cache dir jetpack compose

Iam trying to open a saved pdf file from the internal cache dir.
In the "old" way my code looked liked this.
In jetpack compose i can use this part of code, the pdf is beeining created and i can see it in the device explorer. But how can i display the pdf on screen ?
fun decodeTestPdfString(pdf_string:String, context:Context) {
//make FileOutPutStream
var fos: FileOutputStream? = null
try {
if (pdf_string != null) {
f = File(context?.cacheDir, "testFile" + ".pdf")
f!!.createNewFile()
fos = FileOutputStream(f)
val decodedString: ByteArray = Base64.decode(pdf_string, Base64.DEFAULT)
fos.write(decodedString)
fos.flush()
fos.close()
}
} catch (e: Exception) {
} finally {
if (fos != null) {
fos = null
}
}
val path: Uri = FileProvider.getUriForFile(context!!, context!!.getApplicationContext().getPackageName() + ".provider", f!!)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(path, "application/pdf")
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
}
}
}
But the last step (try intent) is giving an issue.
The file is created in the cache dir, i can see it.
To open the pdf reader:
startActivity(context, intent, options:null)

Pdf intent not showing file

I'm trying to display in the pdf intent of android an file using his Uri, but it seems that I always get an blank pdf and don't know why, can someone give me some advice why.
Code:
private fun pdfConverter(pdfFile: DtoSymptomCheckerPdf?) {
val documentsPath = File(context?.filesDir, "documents")
if(!documentsPath.exists()){
documentsPath.mkdir()
}
val file = File(documentsPath, pdfFile?.filename)
val pdfAsBytes: ByteArray = android.util.Base64.decode(pdfFile?.file, 0)
val os = FileOutputStream(file,false)
os.write(pdfAsBytes)
os.flush()
os.close()
println("EL CONTENT: " + file.length())
val uri = FileProvider.getUriForFile(App.applicationContext,
BuildConfig.APPLICATION_ID + ".provider", file)
val pdfIntent = Intent(Intent.ACTION_VIEW)
pdfIntent.setDataAndType(uri, "application/pdf")
pdfIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
try {
startActivity(pdfIntent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(requireContext(), "Can't read pdf file",
Toast.LENGTH_SHORT).show()
}
}

Categories

Resources