简体   繁体   中英

Import and save a picture from the gallery to the internal memory

I am developing an android application in which I need to import photos from the gallery to save them in the phone's internal memory. I have no idea how to do it, do you have any idea?

I looked on the internet but only came across cases where we wanted to store in the gallery...

Actually, in my application I have objects with a name and an image. For the image, I saved his name (in the drawables) as a String and I will retrieve it by sorting it with the names. I would also like to be able to retrieve images from the phone gallery, but I don't know how to mix the two...

Thank you guy !

You can use Android's ACTION_PICK intent to load an image from a users gallery, see here , with EXTERNAL_CONTENT_URI as the target directory. This will allow the user to select an image using some external app, and provide the URI back to your app once a selection has been made. Please note the code below is in Kotlin.

Somewhere in your Activity, start ACTION_PICK for result:

val intent = Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, 0)

You'll get the URI for the image as data in OnActivityResult , from there you'll need to read the file and write it to your apps storage. Since you're going to load this file into an ImageView as well, I'd suggest re-using the stream. I've included a possible way of doing that in the block below, which is to read the stream into a ByteArray , then write that ByteArray to your FileOutputStream and your ImageView (by using the BitmapFactory class).

See below:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (resultCode == RESULT_OK) {
        val resultUri: Uri? = data?.data

        val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(resultUri))
        val newFile = File(context.filesDir.absolutePath, "aGeneratedFileName.${extension}")

        var inputStream: InputStream? = null
        var byteStream: ByteArrayOutputStream? = null
        var fileOutputStream: FileOutputStream? = null
        var bitmap: Bitmap? = null
        try {
            inputStream = contentResolver.openInputStream(resultUri)
            fileOutputStream = FileOutputStream(newFile)

            IOUtils.copy(inputStream, byteStream)
            var bytes = byteStream.toByteArray()

            fileOutputStream.write(bytes)
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, byteStream.size())
            myImageView.setImageBitmap(bitmap)
        } catch (e: Exception) {
            Log.e(TAG, "Failed to copy image", e)

            inputStream?.close()
            fileOutputStream?.close()
            byteStream?.close()

            return
        } finally {
            inputStream?.close()
            fileOutputStream?.close()
            byteStream?.close()
        }
    } else {
        // Probably handle this error case
    }
}

I'm assuming you'll want to reload the images you've imported when your app next launches, for that you can fetch a list of files in your filesDir and read them using BitmapFactory.decodeFile .

It seems like your goal is to show an array of images, if you haven't already I suggest you look into the RecyclerView class to implement that. If you run into trouble with it, I suggest you open another question, however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM