여기에서 사진을 촬영하고, bitmap을 받아오는 것까지 구현했다.

다음에는 해당 bitmap으로 파일의 uri를 얻어온 뒤, 해당 파일의 path를 찾는 방법을 작성한다.

그냥 getPath 하면 될 줄 알았는데 거지같은 안드로이드 정책때문에.. 

다른 방법으로 접근해야 한다.

 

 

MainActivity.kt

1
2
3
4
5
6
7
8
9
10
11
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            if (myBitmap != null) {
                val myHttp = HttpMultiPart()
                fileUri = getImageUri(this@MainActivity, myBitmap)
                path = RealPath.getRealPathFromURI(this@MainActivity, fileUri)
                myHttp.send(this@MainActivity, path!!)
            }
            else {
                Log.d("uri error", "error")
            }
}
cs

 

myBitmap이 저번에 진행했던 카메라 intent의 결과인 bitmap이다.

HttpMultipart 부분은 서버로 보내는 부분이니 신경쓰지 말고,

getImageUri 메서드를 사용해 파일의 uri를 얻어오고

getRealPathFromURI 메서드를 사용해 파일의 경로를 얻어 해당 경로를 서버로 보내는 방식이다.

 

 

getImageUri

1
2
3
4
5
6
7
8
fun getImageUri(inContext: Context?, inImage: Bitmap?): Uri? {
        val bytes = ByteArrayOutputStream()
        if (inImage != null) {
            inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
        }
        val path = MediaStore.Images.Media.insertImage(inContext?.getContentResolver(), inImage, "Title" + " - " + Calendar.getInstance().getTime(), null)
        return Uri.parse(path)
}
cs

 

val path = MediaStore... 라인에 "Title".. 부분은 자기가 원하는 텍스트를 입력해도 된다.

나는 저장한 시간별로 파일명을 설정하도록 했다.

 

 

getRealPathFromURI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fun getRealPathFromURI(context: Context?, uri: Uri?): String? {
 
        // DocumentProvider
        if (DocumentsContract.isDocumentUri(context, uri)) {
 
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                val docId = DocumentsContract.getDocumentId(uri)
                val splitArray<String?> = docId.split(":".toRegex()).toTypedArray()
                val type = split[0]
                return if ("primary".equals(type, ignoreCase = true)) {
                    (Environment.getExternalStorageDirectory().toString() + "/"
                            + split[1])
                } else {
                    val SDcardpath = getRemovableSDCardPath(context)?.split("/Android".toRegex())!!.toTypedArray()[0]
                    SDcardpath + "/" + split[1]
                }
            } else if (isDownloadsDocument(uri)) {
                val id = DocumentsContract.getDocumentId(uri)
                val contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"),
                        java.lang.Long.valueOf(id))
                return getDataColumn(context, contentUri, nullnull)
            } else if (isMediaDocument(uri)) {
                val docId = DocumentsContract.getDocumentId(uri)
                val splitArray<String?> = docId.split(":".toRegex()).toTypedArray()
                val type = split[0]
                var contentUri: Uri? = null
                if ("image" == type) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                } else if ("video" == type) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
                } else if ("audio" == type) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
                }
                val selection = "_id=?"
                val selectionArgs = arrayOf(split[1])
                return getDataColumn(context, contentUri, selection,
                        selectionArgs)
            }
        } else if (uri != null) {
            if ("content".equals(uri.getScheme(), ignoreCase = true)) {
                // Return the remote address
                return if (isGooglePhotosUri(uri)) uri.getLastPathSegment() else getDataColumn(context, uri, nullnull)
            } else if ("file".equals(uri.getScheme(), ignoreCase = true)) {
                return uri.getPath()
            }
        }
        return null
}
cs

 

코드는 여기

 

Uri 로부터 파일의 실제경로 구하기/외장SD카드의 경로 얻기

안드로이드 앱을 만들다보면 외부의 다운로드 링크나 인터넷 주소가 아니라 기기 내부의 파일을 uri를 사용하여 이용하는 경우가 있습니다. Content Provider를 이용해서 파일에 접근하는 경우에 uri�

featherwing.tistory.com

에서 참고했다.

 

끝.

+ Recent posts