CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2020-12-02
by Jacksonsox

Original Post

Original - Posted on 2017-08-18
by Pankaj Lilan



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I didn't get the integration to the 'Files' app to work, but I did get access to the "Share" to work allowing me to email the file.
Most of what is the Android page and its links as well as many stackoverflow pages [Android Secure Fiile Sharing][1]
**High Level**
1) Add the provider to your manifest 2) Add a res -> xml -> file_path document with the file share 'paths' location 3) Write the code to share the file using the 'paths' location
**Detailed Steps**
1) Update the manifest -> application section with the 'provider' details

<manifest ... <application ... <activity android:name=".ui.MainActivity" android:label="My Toy Box" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.com.YOURDOMAIN.YOURAPP.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>
</manifest>
2) Add a res -> xml -> file_path document with the file share 'paths' location
If you don't already have an xml directory/package under the res location (i.e., where your layouts are, add it and add a file. I called mine file_paths. You may already have one depending on what you already coded. I wrote my document to share to the docs/ folder. This locaiton will be needed in the actual code.
docs/ is located `File(requireContext().filesDir, "docs")` docs/ is located in the emulator under `data/data/com.YOURDOMAIN.YOURAPP/files/docs`
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="my_docs" path="docs/"/> </paths>
3) Write the code to share the file using the 'paths' location and the fileprovider reference

private fun downloadFile() {
val filePath: File = File(requireContext().filesDir, "docs") filePath.mkdirs() val newFile = File(filePath, "MyToyBox.csv") newFile.delete() newFile.createNewFile() val contentUri = FileProvider.getUriForFile( requireContext(), "com.YOURDOMAIN.YOURAP.fileprovider", newFile )
val CSV_HEADER = "Series,My Toy Box ID,Debut Year,Phase,Wave,Action Figure Package Name," + "Movie Scene," + "Estimated New Value,Have the New Figure,Have New Count," + "Want the New Figure,Want New Count," + "Sell the New Figure,Sell New Count," + "Order the New Figure,Order New Count," + "Order from Text," + "Have the Loose Figure,Have Loose Count" + ",Want the Loose Figure,Want Loose Count" + ",Sell the Loose Figure,Sell Loose Count" + ",Notes" + "\n"
var fileWriter: FileWriter? = null
try { fileWriter = FileWriter(newFile)
fileWriter.append(CSV_HEADER)
figureList.forEach { figure ->
val specifics = figure.specifics
val noText = "No" val yesText = "Yes"
val new_haveCount = specifics.new_haveCount.toString() val new_wantCount = specifics.new_wantCount.toString() val new_sellCount = specifics.new_sellCount.toString() val new_orderCount = specifics.new_orderCount.toString() val new_orderText = specifics.new_orderText
val loose_haveCount = specifics.loose_haveCount.toString() val loose_wantCount = specifics.loose_wantCount.toString() val loose_sellCount = specifics.loose_sellCount.toString()
// set yes/no text based on count val new_haveTheFigure = if (specifics.new_haveCount == 0) noText else yesText val new_wantTheFigure = if (specifics.new_wantCount == 0) noText else yesText val new_sellTheFigure = if (specifics.new_sellCount == 0) noText else yesText val new_orderTheFigure = if (specifics.new_orderCount == 0) noText else yesText
val loose_haveTheFigure = if (specifics.loose_haveCount == 0) noText else yesText val loose_wantTheFigure = if (specifics.loose_wantCount == 0) noText else yesText val loose_sellTheFigure = if (specifics.loose_sellCount == 0) noText else yesText
val notes = specifics.notes
// formatted value var newValueString = kUnknownMTBValue val currencyFormat = NumberFormat.getCurrencyInstance() currencyFormat.maximumFractionDigits = 2 currencyFormat.currency = Currency.getInstance("USD") if (figure.saleSummary != null) { val formattedValue = currencyFormat.format(figure.saleSummary!!.estimatedValueMean).toString() newValueString = context?.getString(R.string.saleSummaryValueCount, formattedValue, figure.saleSummary!!.saleCount).toString() }
// need to escape , with \" in front and back, such as in wave and names. val row: String = "\"${figure.series.seriesName}\"," + "${figure.figureUniqueId}," + "${figure.debutYear}," + "\"${figure.phase}\"," + "\"${figure.wave}\"," + "\"${figure.figurePackageName}\"," + "\"${figure.scene}\"," + "\"$newValueString\"," + "$new_haveTheFigure,$new_haveCount," + "$new_wantTheFigure,$new_wantCount," + "$new_sellTheFigure,$new_sellCount," + "$new_orderTheFigure,$new_orderCount," + "\"$new_orderText\"" + ",$loose_haveTheFigure,$loose_haveCount," + "$loose_wantTheFigure,$loose_wantCount," + "$loose_sellTheFigure,$loose_sellCount," + "\"$notes\"" + "\n"
fileWriter.append(row) }
} catch (e: Exception) { println("Writing CSV error!") e.printStackTrace() }
fileWriter!!.close()
createFile(contentUri) }

private fun createFile(pickerInitialUri: Uri) {
val shareIntent: Intent = Intent().apply { action = Intent.ACTION_SEND putExtra(Intent.EXTRA_STREAM, pickerInitialUri) type = "text/plain" setDataAndType(pickerInitialUri, requireContext().contentResolver.getType(pickerInitialUri)) Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION } startActivity(Intent.createChooser(shareIntent, "My Toy Box")) }

[1]: https://developer.android.com/training/secure-file-sharing
If `targetSdkVersion` is higher than **24**, then [FileProvider][1] is used to grant access.
Create an xml file(Path: res\xml\) **provider_paths.xml**
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>
<br>
Add a **Provider** in **[AndroidManifest.xml][2]**
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>
If you are using **androidx**, the FileProvider path should be:
android:name="androidx.core.content.FileProvider"
and **replace**
Uri uri = Uri.fromFile(fileImagePath);
to
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileImagePath);
**Edit:** While you're including the URI with an `Intent` make sure to add below line:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
and you are good to go. Hope it helps.

[1]: https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjiyKWypuDVAhVIso8KHcA2BvIQFgglMAA&url=https%3A%2F%2Fdeveloper.android.com%2Freference%2Fandroid%2Fsupport%2Fv4%2Fcontent%2FFileProvider.html&usg=AFQjCNEBbyss3j2b0k_XQncN1I9vLhrlGQ [2]: https://developer.android.com/guide/topics/manifest/manifest-intro.html

        
Present in both answers; Present only in the new answer; Present only in the old answer;