Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

kotlin - Android image compression - Zelory Compressor doesn't upload image with special character on file path

I'm using Zelory Compressor to compress an image before uploading it to the server. It works, unless the image file path has special characters in the beginning or in the middle of it (e.g. ?, ~, ^, etc.) I'd like to fix that since it's a Brazilian Portuguese (pt_BR) app, instead of just asking the users to upload images with a valid path.

My dependency:

implementation 'id.zelory:compressor:2.1.0'

My compressor method:

fun compress(context: Context, path: String) : MultipartBody.Part {
        var file = File(path)
        try {
            file = Compressor(context)
                .setMaxWidth(1080)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .compressToFile(file)
        } catch (ex: Exception) {
            Log.d("Error : ", "Erro upload: ${ex.message}")
        }

        return MultipartBody.Part.createFormData(
            "document",
            file.name,
            RequestBody.create(MediaType.parse("image/*"), file)
        )
    }

StackTrace:

Caused by: java.lang.IllegalArgumentException: Unexpected char 0xe7 at 91 in Content-Disposition value: form-data; name="document"; filename="Teste acentua??o de línguas latinas.png"
    at okhttp3.Headers.checkValue(Headers.java:272)
    at okhttp3.Headers.of(Headers.java:224)
    at okhttp3.MultipartBody$Part.createFormData(MultipartBody.java:259)
    at br.com.company.util.CameraUtils$Companion.compress(CameraUtils.kt:196)
    at br.com.company.viewmodel.RefundAttachFilesViewModel.uploadDocument(RefundAttachFilesViewModel.kt:29)
    at br.com.company.ui.refund.RefundAttachFilesActivity.onActivityResult(RefundAttachFilesActivity.kt:132)
    at android.app.Activity.dispatchActivityResult(Activity.java:7454)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4384)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4433)?
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)?
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)?
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1836)?
    at android.os.Handler.dispatchMessage(Handler.java:106)?
    at android.os.Looper.loop(Looper.java:193)?
    at android.app.ActivityThread.main(ActivityThread.java:6702)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...