Skip to content

Commit

Permalink
reorganised android files
Browse files Browse the repository at this point in the history
  • Loading branch information
pratyaksh1610 committed Jul 13, 2022
1 parent 2876943 commit d6c2b81
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 93 deletions.
79 changes: 0 additions & 79 deletions MemeShare/app/src/main/java/com/example/memeshare/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,83 +112,4 @@ class MainActivity : AppCompatActivity() {
val chooser = Intent.createChooser(intent,"Share this meme using...")
startActivity(chooser)
}

fun saveImg(view: View){
// on click of this btnSve button it will capture
// screenshot of imgView and save it to gallery
btnSave.setOnClickListener {
// extract the bitmap of the imgView using
// getBitmapOfImgView
val bitmap = getBitmapOfImgView(imgView)
// if bitmap is not null then
// save it to gallery
if (bitmap != null) {
saveImgViewToStorage(bitmap)
}
}
}

private fun getBitmapOfImgView(v: View): Bitmap? {
// create a bitmap object
var screenshotOfImgView: Bitmap? = null
try {
// inflate screenshot object
// with Bitmap.createBitmap it
// requires three parameters
// width and height of the view and
// the background color
screenshotOfImgView = Bitmap.createBitmap(v.measuredWidth, v.measuredHeight, Bitmap.Config.ARGB_8888)
// Now draw this bitmap on a canvas
val canvas = Canvas(screenshotOfImgView)
v.draw(canvas)
} catch (e: Exception) {
Log.e("Error", "Failed to capture ! " + e.message)
}
// return the bitmap
return screenshotOfImgView
}


// this method saves the image to gallery
private fun saveImgViewToStorage(bitmap: Bitmap) {
// Generating a file name
val filename = "${System.currentTimeMillis()}.jpg"

// Output stream
var fos: OutputStream? = null

// For devices running android >= Q
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// getting the contentResolver
this.contentResolver?.also { resolver ->

// Content resolver will process the contentvalues
val contentValues = ContentValues().apply {

// putting file information in content values
put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
}

// Inserting the contentValues to
// contentResolver and getting the Uri
val imageUri: Uri? = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

// Opening an outputstream with the Uri that we got
fos = imageUri?.let { resolver.openOutputStream(it) }
}
} else {
// These for devices running on android < Q
val imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
val image = File(imagesDir, filename)
fos = FileOutputStream(image)
}

fos?.use {
// Finally writing the bitmap to the output stream that we opened
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
Toast.makeText(this , "Saved to Gallery ! \uD83C\uDF89" , Toast.LENGTH_SHORT).show()
}
}
}
14 changes: 0 additions & 14 deletions MemeShare/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,4 @@
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />

<!-- save button-->
<ImageView
android:id="@+id/btnSave"
app:srcCompat="@drawable/share_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:onClick="saveImg"
android:layout_marginBottom="90dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.956"
app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit d6c2b81

Please sign in to comment.