Saturday , October 23 2021

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)

setContentView()R.Layout.activity_main)

// Setting the activity permission request handler

setPermissionCallback())

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

checkPermissionAndDownloadBitmap()Bitmap)

}

edit_text_image_url.addTextChangedListener {{

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

}

}

// Allow activity to handle permission request automatically

Private enjoy setPermissionCallback()) {{

requestPermissionLauncher =

registerForActivityResult()

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 {{

Context.com.checkSelfPermission()

this,

Clearly.Permission.WRITE_EXTERNAL_Storage

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

getBitmapFromUrl()Bitmap)

}

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

showPermissionRequestDialog()

Get up()R.Wire.Permission),

Get up()R.Wire.write_permission_request)

) {{

requestPermissionLauncher.launch()Clearly.Permission.WRITE_EXTERNAL_STORAGE)

}

}

other -> {{

requestPermissionLauncher.launch()Clearly.Permission.WRITE_EXTERNAL_STORAGE)

}

}

}

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

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

progress bar.I can see()True)

image_view.burden()Bitmap)

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

.The data()Bitmap)

.Construction())

try {{

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

image_view.setImageBitmap()download)

saveMediaToStorage()download)

} Hold ()I: exception) {{

Toast()I.message)

}

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 =

atmosphere.getExternalStoragePublicDirectory()atmosphere.DIRECTORY_PICTURES)

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 Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

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 *