Android Save Bitmap to Gallery – Android Coding by DMTechnolab

class Main activity : AppCompatActivity()) {{

// ImageLoader example

Private Latite Where Image maker: ImageLoader

// permission request handler

Private Latite Where requestPermissionLauncher: Activity ResultLauncher<String>

Override enjoy Go ahead()saveInstanceState: BundleThe) {{

Super.Go ahead()saveInstanceState)


// Setting the activity permission request handler


progress bar.I can see()false)

// get imaginative examples

Image maker = The coil.Image maker()this)

button_paste_link.setOnClickListener {{

Paste link())


Button_download.setOnClickListener {{

Val Bitmap = edit_text_image_url.The text.Wire()).Trim())

// The check permission is suppressed when downloaded and the bitmap is saved from the URL



edit_text_image_url.addTextChangedListener {{

Button_download.Enable()this.Wire()).Is not empty()))



// Allow activity to handle permission request automatically

Private enjoy setPermissionCallback()) {{

requestPermissionLauncher =


ActivityResultContracts.request permission())

) {{ Approved: Boolean ->

If ()Approved) {{

getBitmapFromUrl()edit_text_image_url.The text.Wire()).Trim()))




// Function to work and request storage permission

Private enjoy checkPermissionAndDownloadBitmap()Bitmap: String) {{

When {{



) == package manager.permission granted -> {{



shouldShowRequestPermissionRationale()Clearly.Permission.WRITE_EXTERNAL_STORAGE) -> {{


Get up()R.Wire.Permission),

Get up()R.Wire.write_permission_request)

) {{




other -> {{





// This function will fetch the bitmap from the given URL

Private enjoy getBitmapFromUrl()Bitmap: String) = Life Cycle.launch {{

progress bar.I can see()True)


Val Request = ImageRequest.Builder()this@Main activity)

.The data()Bitmap)


try {{

Val download = ()Image maker.Executed()Request).Attractible As Bitmap).Bitmap



} Hold ()I: exception) {{



progress bar.I can see()false)


// The function I already explained is used to save the bitmap to external storage

Private enjoy saveMediaToStorage()Bitmap: Bitmap) {{

Val file name = “$ {System.currentTimeMillis ()}. Jpg”

Where Fos: OutputstreamThe = Invalid

If ()the construction of.the version.SDK_INT > = the construction of.VERSION_CODES.Q) {{

ContentThe.Too {{ solve out ->

Val Content = Content()).Applicable {{

Branch()Mediastore.Mediacolumns.display name, file name)

Branch()Mediastore.Mediacolumns.mime type, “Image / jpg”)

Branch()Mediastore.Mediacolumns.relative path, atmosphere.DIRECTORY_PICTURES)


Val imageUri: to hateThe =

solve out.Include()Mediastore.Images.Media.EXTERNAL_CONTENT_URI, Content)

Fos = imageUriThe.Let’s go {{ solve out.openOutputStream()this) }


} other {{

Val Picture =


Val The image = The file()Picture, file name)

Fos = FileOutputStream()The image)


FosThe.Use {{

Bitmap.Compress()Bitmap.Compress.Jpeg, 100, this)

Toast()“Saved for photo”)



// Paste the value of EditText from the clipboard

Private enjoy Paste link()) {{

Val Clipboard: Clipboard managerThe =

getSystemService()Context.CLIPBOARD_SERVICE) As Clipboard managerThe

If ()ClipboardThe.hasPrimaryClip()) == True) {{

edit_text_image_url.Settext()Clipboard.PrimaryclipThe.getItemAt()0)The.The text.Wire()))




Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

One comment

  1. NYC NY Commercial moving companies

    Howdy! Brief dilemma that’s totally off subject matter. Do you know the way to help make your site cellular pleasant? My web site looks Odd when browsing from my apple iphone. I’m making an attempt to find a topic or plugin That may be capable of solve this problem. For those who have any suggestions, you should share. Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *