0
votes

Convertir bitmap en uri à l'intérieur d'un fragment de Kotlin

J'ai stocké le bitmap à partir de la capture de la caméra dans la variable photo. Maintenant, je veux le convertir en URI afin de pouvoir le stocker dans un fichier, puis de le stocker sur mon serveur.

Voici comment je stocke l'image bitmap: strong> p> xxx pré>

J'utilise ceci: photo = data.getextras (). Obtenez ("Données") comme bitmap Pour stocker mon image et l'afficher en utilisant ceci: imageview.setimagebitmap (photo) Ça fonctionne bien. P>

Mais je veux utiliser cette fonction: FilePath = getImageFilePath (données) Pour obtenir l'URI et le stocker comme URI dans l'OUPUTFILEURI, puis comme fichier dans FilePath. P>

Voici comment getImageFilePath fonctionne: strong> p>

   fun getImageFilePath(data: Intent): String {
        return getImageFromFilePath(data)
    }

    private fun getImageFromFilePath(data: Intent?): String {
        val isCamera = data == null || data.data == null

        return if (isCamera)
            getCaptureImageOutputUri()!!.getPath()
//            getCaptureImageOutputUri()!!.getAbsolutePath()
        else
            getPathFromURI(data!!.data)

    }

    private fun getCaptureImageOutputUri(): Uri? {
        val getImage = activity!!.getExternalFilesDir("")
        if (getImage != null) {
            outputFileUri = Uri.fromFile(File(getImage.path, "profile.png"))
//            outputFileUri= getImageUriFromBitmap(activity!!,photo!!)
        }
        return outputFileUri
    }


0 commentaires

3 Réponses :


0
votes

Lors de la mise à jour de l'intention, vous pouvez passer un supplément comme MediaStore.extra_Output comme emplacement de fichier et ultérieurement, vous obtiendrez URI dans le résultat d'activité comme URI. Réf: https://stackoverflow.com/questions/2729267/androïd-camera-intent code>

et si vous voulez URI dans le fichier, vous pouvez utiliser FileProvider pour obtenir son URI. P >

 Uri uri = FileProvider.getUriForFile(context, "com.package.name.fileprovider", file);


0 commentaires

0
votes
// Get the image from drawable resource as drawable object
// Get the bitmap from drawable object

// Initializing a new file
// The bellow line return a directory in internal storage
var file: File = Environment.getExternalStoragePublicDirectory("yourFolderName")

if (!file.exists()) {
    // Create a file to save the image
    file.mkdirs()
}

file = File(file, "yourPicName")
try {
    // Get the file output stream
    val stream: OutputStream = FileOutputStream(file)

    stream.use {
        // Compress bitmap
        this.compress(Bitmap.CompressFormat.JPEG, 100, it)

        // Flush the stream
        it.flush()

        // Close stream
        it.close()

        val uri:Uri = file.toUri()

        // OR

        val uri2:Uri = Uri.fromFile(file)
    }
} catch (e: IOException) { // Catch the exception
    e.printStackTrace()
}

0 commentaires

0
votes