-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] MutiPartResolver 생성과 post 보류 (#71)
- Loading branch information
Showing
4 changed files
with
116 additions
and
40 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
core/src/main/java/com/photosurfer/android/core/util/MutiPartResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.photosurfer.android.core.util | ||
|
||
import android.content.Context | ||
import android.database.Cursor | ||
import android.net.Uri | ||
import android.provider.DocumentsContract | ||
import android.provider.MediaStore | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.MultipartBody | ||
import okhttp3.RequestBody.Companion.asRequestBody | ||
import java.io.File | ||
|
||
class MutiPartResolver { | ||
fun createImgMultiPart(uri: Uri, context: Context): MultipartBody.Part { | ||
val file = File((getPath(context, uri).toString())) | ||
val surveyBody = file.asRequestBody("image/*".toMediaTypeOrNull()) | ||
return MultipartBody.Part.createFormData("image", file.name, surveyBody) | ||
} | ||
|
||
private fun getPath(context: Context, uri: Uri): String? { | ||
val docId = DocumentsContract.getDocumentId(uri) | ||
val split = docId.split(":".toRegex()).toTypedArray() | ||
val contentUri: Uri? = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | ||
val selection = "_id=?" | ||
val selectionArgs = arrayOf(split[1]) | ||
return getDataColumn(context, contentUri!!, selection, selectionArgs) | ||
} | ||
|
||
private fun getDataColumn( | ||
context: Context, | ||
uri: Uri, | ||
selection: String?, | ||
selectionArgs: Array<String>? | ||
): String? { | ||
var cursor: Cursor? = null | ||
val column = "_data" | ||
val projection = arrayOf(column) | ||
try { | ||
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) | ||
if (cursor != null && cursor.moveToFirst()) { | ||
val columnIndex: Int = cursor.getColumnIndexOrThrow(column) | ||
return cursor.getString(columnIndex) | ||
} | ||
} finally { | ||
cursor?.close() | ||
} | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters