Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
feat: added base64encodedstring value on return response (android)
Browse files Browse the repository at this point in the history
  • Loading branch information
killi8n committed Nov 4, 2020
1 parent bbb08d4 commit f7d4982
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import android.provider.OpenableColumns
import android.util.Base64
import android.webkit.MimeTypeMap
import androidx.core.content.FileProvider
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.PermissionAwareActivity
import com.facebook.react.modules.core.PermissionListener
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.util.*


Expand Down Expand Up @@ -120,20 +126,20 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
if (!it.isFinishing) {
val dialogBuilder = AlertDialog.Builder(it)
.setTitle("사진 선택")
.setItems(arrayOf("사진 촬영", "앨범에서 가져오기")) { dialog, which ->
.setItems(arrayOf("사진 촬영", "앨범에서 가져오기")) { _, which ->
if (which == 0) {
this.checkCameraPermission()
}
if (which == 1) {
this.checkLibraryPermission()
}
}
.setNeutralButton("취소", { dialog, which ->
.setNeutralButton("취소") { _, _ ->
val error = Arguments.createMap()
error.putString("error", "USER_CACNEL")
callback(error)
this.globalCallback = null
})
}
dialogBuilder.show()
}
}
Expand Down Expand Up @@ -177,22 +183,47 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
private var callbackInvoker: Callback? = callback
private var context: ReactApplicationContext? = context

private fun encodeImage(path: String): String? {
val imagefile = File(path)
var fis: FileInputStream? = null
try {
fis = FileInputStream(imagefile)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
val bm = BitmapFactory.decodeStream(fis)
val baos = ByteArrayOutputStream()
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val b: ByteArray = baos.toByteArray()
//Base64.de
return Base64.encodeToString(b, Base64.NO_WRAP)
}

override fun onActivityResult(activity: Activity?, requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(activity, requestCode, resultCode, data)
if (requestCode == IMAGE_CAPTURE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
cameraCaptureFile.let { cameraCaptureFile ->
if (cameraCaptureFile != null) {
val uriString: String = "file://" + Uri.fromFile(cameraCaptureFile).path
val path: String? = Uri.fromFile(cameraCaptureFile).path
val uriString = "file://$path"
val fileSize: Long = cameraCaptureFile.length()
val type: String = cameraCaptureFile.extension
val fileName: String = cameraCaptureFile.name
var base64EncodedString: String? = null
path.let { parsedPath ->
if (parsedPath != null) {
base64EncodedString = this.encodeImage(parsedPath)
}
}
this.callbackInvoker.let { callback ->
if (callback != null) {
var response = Arguments.createMap()
val response = Arguments.createMap()
response.putString("path", path)
response.putString("uri", uriString)
response.putDouble("fileSize", fileSize.toDouble())
response.putString("type", "image/" + type)
response.putString("type", "image/$type")
response.putString("fileName", fileName)
response.putString("data", base64EncodedString)
callback.invoke(null, response)
this.callbackInvoker = null
}
Expand All @@ -219,13 +250,22 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
val displayNameColumnIndex = parsedCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)
val fileSize = parsedCursor.getLong(sizeColumnIndex)
val fileName = parsedCursor.getString(displayNameColumnIndex)
val realPath = "file://" + PathManager.getPathFromURI(parsedContext, parsedUri)
var type = MimeTypeMap.getFileExtensionFromUrl(realPath)
var response = Arguments.createMap()
response.putString("uri", realPath)
val path = PathManager.getPathFromURI(parsedContext, parsedUri)
val uriString = "file://$path"
val type = MimeTypeMap.getFileExtensionFromUrl(uriString)
var base64EncodedString: String? = null
path.let { parsedPath ->
if (parsedPath != null) {
base64EncodedString = this.encodeImage(parsedPath)
}
}
val response = Arguments.createMap()
response.putString("path", path)
response.putString("uri", uriString)
response.putDouble("fileSize", fileSize.toDouble())
response.putString("type", "image/" + type)
response.putString("type", "image/$type")
response.putString("fileName", fileName)
response.putString("data", base64EncodedString)
callback.invoke(null, response)
this.callbackInvoker = null
parsedCursor.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.reactnativeimageselector

import java.util.Arrays
import java.util.Collections

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager
import com.facebook.react.bridge.JavaScriptModule

class ImageSelectorPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.ReactContext;

public class PathManager {
Expand Down

0 comments on commit f7d4982

Please sign in to comment.