|
1 | 1 | package com.simplemobiletools.filemanager.pro.activities |
2 | 2 |
|
3 | 3 | import android.content.Intent |
| 4 | +import android.net.Uri |
4 | 5 | import android.os.Bundle |
5 | | -import com.simplemobiletools.commons.extensions.toast |
| 6 | +import com.simplemobiletools.commons.dialogs.FilePickerDialog |
| 7 | +import com.simplemobiletools.commons.extensions.* |
| 8 | +import com.simplemobiletools.commons.helpers.ensureBackgroundThread |
6 | 9 | import com.simplemobiletools.filemanager.pro.R |
| 10 | +import com.simplemobiletools.filemanager.pro.extensions.config |
| 11 | +import java.io.File |
7 | 12 |
|
8 | 13 | class SaveAsActivity : SimpleActivity() { |
9 | 14 | override fun onCreate(savedInstanceState: Bundle?) { |
10 | 15 | super.onCreate(savedInstanceState) |
11 | 16 | setContentView(R.layout.activity_save_as) |
12 | 17 |
|
13 | 18 | if (intent.action == Intent.ACTION_SEND && intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) { |
| 19 | + FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden, showFAB = true) { |
| 20 | + val destination = it |
| 21 | + handleSAFDialog(destination) { |
| 22 | + toast(R.string.saving) |
| 23 | + ensureBackgroundThread { |
| 24 | + try { |
| 25 | + if (!getDoesFilePathExist(destination)) { |
| 26 | + if (needsStupidWritePermissions(destination)) { |
| 27 | + val document = getDocumentFile(destination) |
| 28 | + document!!.createDirectory(destination.getFilenameFromPath()) |
| 29 | + } else { |
| 30 | + File(destination).mkdirs() |
| 31 | + } |
| 32 | + } |
14 | 33 |
|
| 34 | + val source = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM) |
| 35 | + val mimeType = source!!.toString().getMimeType() |
| 36 | + val inputStream = contentResolver.openInputStream(source) |
| 37 | + val filename = source.toString().getFilenameFromPath() |
| 38 | + |
| 39 | + val destinationPath = "$destination/$filename" |
| 40 | + val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!! |
| 41 | + inputStream!!.copyTo(outputStream) |
| 42 | + rescanPaths(arrayListOf(destinationPath)) |
| 43 | + toast(R.string.file_saved) |
| 44 | + finish() |
| 45 | + } catch (e: Exception) { |
| 46 | + showErrorToast(e) |
| 47 | + finish() |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
15 | 52 | } else { |
16 | 53 | toast(R.string.unknown_error_occurred) |
17 | 54 | finish() |
|
0 commit comments