Pick from Google Photos provider with ACTION_GET_CONTENT or OPEN_DOCUMENT

后端 未结 4 1971
死守一世寂寞
死守一世寂寞 2021-01-17 18:33

I have no clue at why this happens, but I am not able to pick images from the Google Photos provider. Testing on API 27.

With ACTION_GET_CONTENT

If I use:<

4条回答
  •  旧时难觅i
    2021-01-17 18:58

    I've done it with this code.

    val intent = Intent(Intent.ACTION_GET_CONTENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    intent.type = "image/*"
    startActivityForResult(intent,100)
    

    and for result

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            100 -> {
                if (resultCode == Activity.RESULT_OK) {
                    val bitmap = generateBitmap(this,data!!.data)
                    //TODO show bitmap to your View
                    showPicture(bitmap!!)
                }
            }
        }
    }
    

    generateBitmap(context,uri) method in Kotlin

    fun generateBitmap(context: Context, uri: Uri): Bitmap? {
        var filePath = ""
        var cursor: Cursor?
        var columnIndex = 0
        try {
            val column = arrayOf(MediaStore.Images.Media.DATA)
            val sel = arrayOf(MediaStore.Images.Media._ID + "=?")
            if (uri.toString().startsWith("content://com.google.android.apps.photos.contentprovider")){
                val content = this.contentResolver.openInputStream(uri) ?: return null
                val pictureBitmap = BitmapFactory.decodeStream(content)
                return pictureBitmap
            } else {
                filePath = ""
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    val wholeID = DocumentsContract.getDocumentId(uri)
                    val id = arrayOf(wholeID.split(":")[1])
                    cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel[0], id, null)
                    columnIndex = cursor.getColumnIndex(column[0])
                    if (cursor.moveToFirst()) {
                        filePath = cursor.getString(columnIndex)
                    }
                    cursor.close()
                } else {
                    val cursorLoader = CursorLoader(context, uri, column, null, null, null)
                    val cursor = cursorLoader.loadInBackground()
                    if (cursor != null) {
                        var column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
                        cursor.moveToFirst()
                        filePath = cursor.getString(column_index)
                    }
                }
                //TODO generate bitmap from file path
                return bitmap(filePath)
            }
        } catch (e: Exception) {
            print(e.message)
        }
        return null
    }
    

    for getBitmap from file path I used this method

     fun bitmap(path : String) : Bitmap{
        val image = File(path)
        val bmOptions = BitmapFactory.Options()
        return BitmapFactory.decodeFile(image.absolutePath, bmOptions)
    }
    

提交回复
热议问题