Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload DocumentFile to server #41

Closed
sagarnayak opened this issue Jul 14, 2021 · 5 comments
Closed

Upload DocumentFile to server #41

sagarnayak opened this issue Jul 14, 2021 · 5 comments
Labels
question Further information is requested

Comments

@sagarnayak
Copy link

sagarnayak commented Jul 14, 2021

Library version: 0.9.0
OS version: [Android 10]
Device model: [Vivo v11 Pro]

Describe the bug
Getting getAbsolutePath{} as blank but the name is there.

To Reproduce

implementation "com.anggrayudi:storage:0.9.0"

private lateinit var storage: SimpleStorageHelper

storage = SimpleStorageHelper(this)
	storage.onStorageAccessGranted = { _, root ->
		Toast.makeText(
			this,
			getString(
				R.string.ss_selecting_root_path_success_without_open_folder_picker,
				root.getAbsolutePath(this)
			),
			Toast.LENGTH_SHORT
		).show()
	}
		
storage.onFileSelected = { _, file ->
	Toast.makeText(baseContext, "File selected: ${file.fullName}", Toast.LENGTH_SHORT)
	.show()
	val fileLocation = file.getAbsolutePath(this@UploadDocument)
	logUtil.logV(">>>>> the name is : ${file.name}")
	logUtil.logV(">>>>> if file : ${file.isFile}")
	logUtil.logV(">>>>> the location : $fileLocation")
	gotFile(
		File(fileLocation)
	)
}

storage.openFilePicker(REQUEST_CODE_PICK_FILE, "image/*", "application/pdf")

Stacktrace

V: >>>>> the name is : IMG-20210712-WA0000.jpg
V: >>>>> if file : true
V: >>>>> the location : 
@anggrayudi
Copy link
Owner

anggrayudi commented Jul 14, 2021

DocumentFile.getAbsolutePath() returns empty string on some conditions. Usually, it happens when the DocumentFile is androidx.documentfile.provider.SingleDocumentFile because its URI does not contain any file path. The file path of SingleDocumentFile is stored in the SAF database. To access it, you can convert the file to MediaFile:

val media = documentFile.toMediaFile(context)
val path = media.absolutePath

But there's no guarantee that the SAF will give you the actual path. Personally, I avoid using the absolute path for file management. Normally, an absolute path is used to show useful information to the user, whereas managing files is always with URI.

@anggrayudi anggrayudi added the question Further information is requested label Jul 14, 2021
@sagarnayak
Copy link
Author

I want to get the absolute path to create a file. I use that file to upload the it to server.

What would you suggest to do here to be able to upload that file to the server?

@anggrayudi
Copy link
Owner

anggrayudi commented Jul 15, 2021

You can read the file via InputStream:

storage.onFileSelected = { _, files: List<DocumentFile> ->
    files.first().openInputStream(context)?.use { stream ->
       // upload stream to the server in Base64 format
       // https://stackoverflow.com/q/42667942/3922207
    }
}

@sagarnayak
Copy link
Author

I also do validation for file type explicitly - for image types and pdf. (can be done with file name)
and a size check which is an issue as of now.
show a preview to the user. (I think this can be done with input stream)
upload to server. (I think can be done with input stream)

can you suggest any method that can give me the size of the file in any way?

@anggrayudi
Copy link
Owner

anggrayudi commented Jul 15, 2021

  • For file type, better use DocumentFile.type or DocumentFile.mimeTypeByFileName.
  • For file size, use DocumentFile.length() or DocumentFile.getFormattedSize()

@anggrayudi anggrayudi changed the title Not getting file absoulte path Upload DocumentFile to server Sep 8, 2021
@anggrayudi anggrayudi pinned this issue May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants