Save a file(zip) to specify folder in Android 10 - android

I want create a file to specify folder ,my file type is zip,I want use
FileOutStream to create.
It sends an intent, you can see the code below:
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(mContext,intent, 9999,null)
then it gets the returned data, you can see the code below:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {
9999 -> {
Log.v("Test", "Result URI " + data!!.getData())
// FileInputStream(data!!.getData())
}
}
}
}
}
the log is
V/Test: Result URI content://com.android.externalstorage.documents/tree/primary%3ATestFile
I want to create file with the Uri,but use FileOutStream will crash,have any way to create file in specify folder?

Related

How to load audio file in ListView?

I need to load random audio files from storage, into my ListView, on a button click. But I don't understand how can I do this. When I call intent, file manager open to pick file. My song must have title, artist name and url. But after clicking it doesn't add. This is my code. Don't scold me for a possibly dull question. I'm new at android developing.
private fun loadSong() {
val intent: Intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "audio/*"
action = Intent.ACTION_GET_CONTENT
}
startActivityForResult(Intent.createChooser(intent, "Select Audio"), PICK_AUDIO)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_AUDIO && resultCode == RESULT_OK){
if (data != null) {
audioUri = data.data
}
}
adapter = SongsAdapter(listSongs)
twoTracksListView.adapter = adapter
}

I can't delete the folder's content from path

I'm currently getting this path from my FileExplorer
val path = "content://com.android.externalstorage.documents/tree/primary:Download"
And I'm trying to delete the content of this location:
val fileFolder = File(path)
deleteFolderContent(fileFolder)
private fun deleteFolderContent(fileFolder: File) {
val files = fileFolder.listFiles()
if (files.isNullOrEmpty()) {
return
} else {
for (file in files) {
file.delete()
}
}
}
But files is always null and I can't delete the content. What am I doing wrong? Can anyone help me? Thanks.
Update:
For obtain this "path" I did this:
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, SELECT_FOLDER_REQUEST_CODE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SELECT_FOLDER_REQUEST_CODE && resultCode == RESULT_OK) {
val uriTree = data?.data
}
}
you can't access content directory as a File (even when URI points on local storage). check out Providers and ContentResolver, HERE you have some basics, for files there is special one: FileProvider. but for your case ContentResolver may fit for your needs best, with method:
context.contentResolver.delete(uriToDelete, null, null)

How to open a file explorer and get selected file path using implicit intent kotlin

I want to open a file explorer and get selected file path in Kotlin Is there any guid for this purpose?
The most simple option to show a file explorer is to call to the "ACTION_GET_CONTENT" intent and get a code result (777 at the example) with "startActivityForResult", like this:
val intent = Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select a file"), 777)
Later, override the "onActivityResult" function in your activity and get the data only if the request code is the same that the "startActivityForResult" (777 at the example).
To save the URI from the selected file you can get "data?.data.toString()" and if you only need the path, use "data?.data?.path"
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 777) {
val filePath = data?.data?.path
}
}

How to read json file from path using kotlin

I'm trying to open a file picker in android, select a json file then get the text from it.
The app crashes after i select the file because it can not find the path.
I have tried adding external storage read / write permission and changing the path format
/// some Activity code
val myFileIntent=Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(myFileIntent,10)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val path = data!!.data.path
val jsonContent = File(path).readText()
}
I'm trying to open a file picker in android
That is not a file picker. That allows the user to choose a piece of content, which may or may not be a file.
The app crashes after i select the file
You are not picking a file. You are picking a piece of content. That content is identified by a Uri, and the scheme of your Uri is content, not file.
Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. You can call readText() on that InputStream to read it in as text.
This is my final working code :
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val path = data?.data
val jsonSelectedFile = contentResolver.openInputStream(path);
val inputAsString = jsonSelectedFile.bufferedReader().use { it.readText() }
Toast.makeText(this, "Json: " + inputAsString , Toast.LENGTH_LONG).show()
}
And for calling :
importJsonButton.setOnClickListener {
val myFileIntent = Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(myFileIntent,10)
}

Edit image in Android

I'm want to do something like whatsapp, when user capture an image or select an image from gallery, they can edit the image/ draw something on the image.
Any help will be appreciated.
There are so many libraries for that.
Just try this Whatsapp like photo editor
Checkout the sample and library
ImageEditor.Builder(this, imagePath)
.setStickerAssets("stickers")
.disable(ImageEditor.EDITOR_TEXT) //to disable something
.open()
You will get the result in onActivityResult
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
ImageEditor.RC_IMAGE_EDITOR ->
if (resultCode == Activity.RESULT_OK && data != null) {
val imagePath: String = data.getStringExtra(ImageEditor.EXTRA_EDITED_PATH)
edited_image.setImageBitmap(BitmapFactory.decodeFile(imagePath))
}
}
}

Categories

Resources