@@ -4,14 +4,9 @@ import android.app.Activity
44import android.content.ActivityNotFoundException
55import android.content.Context
66import android.content.Intent
7- import android.graphics.Bitmap
8- import android.graphics.Rect
97import android.net.Uri
108import android.os.Bundle
11- import android.os.Handler
12- import android.os.Looper
139import android.util.Size
14- import android.view.PixelCopy
1510import android.widget.Toast
1611import androidx.activity.addCallback
1712import androidx.activity.viewModels
@@ -25,6 +20,7 @@ import com.google.android.material.datepicker.DateValidatorPointBackward
2520import com.google.android.material.datepicker.MaterialDatePicker
2621import com.teampophory.pophory.BuildConfig
2722import com.teampophory.pophory.R
23+ import com.teampophory.pophory.common.bitmap.capture
2824import com.teampophory.pophory.common.bitmap.saveToDisk
2925import com.teampophory.pophory.common.context.colorOf
3026import com.teampophory.pophory.common.context.snackBar
@@ -104,53 +100,31 @@ class AddPhotoActivity : AppCompatActivity() {
104100 finish()
105101 }
106102 binding.btnShare.setOnSingleClickListener {
107- val bitmap = Bitmap .createBitmap(
108- binding.layoutImage.measuredWidth,
109- binding.layoutImage.measuredHeight,
110- Bitmap .Config .ARGB_8888 ,
111- )
112- val location = IntArray (2 )
113- binding.layoutImage.getLocationInWindow(location)
114- PixelCopy .request(
115- window,
116- Rect (
117- location[0 ],
118- location[1 ],
119- location[0 ] + binding.layoutImage.width,
120- location[1 ] + binding.layoutImage.height,
121- ),
122- bitmap,
123- {
124- if (it == PixelCopy .SUCCESS ) {
125- lifecycleScope.launch {
126- val shareImageUri = bitmap.saveToDisk(this @AddPhotoActivity)
127- val intent = Intent (" com.instagram.share.ADD_TO_STORY" ).apply {
128- putExtra(" source_application" , BuildConfig .FACEBOOK_APP_ID )
129- type = " image/png"
130- putExtra(" interactive_asset_uri" , shareImageUri)
131- putExtra(" top_background_color" , " #000000" )
132- putExtra(" bottom_background_color" , " #000000" )
133- }
134- grantUriPermission(
135- " com.instagram.android" ,
136- shareImageUri,
137- Intent .FLAG_GRANT_READ_URI_PERMISSION ,
138- )
139- try {
140- startActivity(intent)
141- } catch (e: ActivityNotFoundException ) {
142- Toast .makeText(
143- this @AddPhotoActivity,
144- " 인스타그램 앱이 존재하지 않습니다." ,
145- Toast .LENGTH_SHORT ,
146- )
147- .show()
148- }
149- }
150- }
151- },
152- Handler (Looper .getMainLooper()),
153- )
103+ lifecycleScope.launch {
104+ val bitmap = binding.layoutImage.capture(this @AddPhotoActivity)
105+ val shareImageUri = bitmap.saveToDisk(this @AddPhotoActivity)
106+ val intent = Intent (" com.instagram.share.ADD_TO_STORY" ).apply {
107+ putExtra(" source_application" , BuildConfig .FACEBOOK_APP_ID )
108+ type = " image/png"
109+ putExtra(" interactive_asset_uri" , shareImageUri)
110+ putExtra(" top_background_color" , " #000000" )
111+ putExtra(" bottom_background_color" , " #000000" )
112+ }
113+ grantUriPermission(
114+ " com.instagram.android" ,
115+ shareImageUri,
116+ Intent .FLAG_GRANT_READ_URI_PERMISSION ,
117+ )
118+ try {
119+ startActivity(intent)
120+ } catch (e: ActivityNotFoundException ) {
121+ Toast .makeText(
122+ this @AddPhotoActivity,
123+ " 인스타그램 앱이 존재하지 않습니다." ,
124+ Toast .LENGTH_SHORT ,
125+ ).show()
126+ }
127+ }
154128 }
155129 }
156130
0 commit comments