Skip to content

Commit 7fcac73

Browse files
committed
Add feature: Share photo to instagram story
1 parent 091d9fe commit 7fcac73

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

app/src/main/java/com/teampophory/pophory/feature/home/photo/AddPhotoActivity.kt

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.teampophory.pophory.feature.home.photo
22

33
import android.app.Activity
4+
import android.content.ActivityNotFoundException
45
import android.content.Context
56
import android.content.Intent
7+
import android.graphics.Bitmap
8+
import android.graphics.Rect
69
import android.net.Uri
710
import android.os.Bundle
11+
import android.os.Handler
12+
import android.os.Looper
813
import android.util.Size
14+
import android.view.PixelCopy
15+
import android.widget.Toast
916
import androidx.activity.addCallback
1017
import androidx.activity.viewModels
1118
import androidx.appcompat.app.AppCompatActivity
@@ -18,6 +25,7 @@ import com.google.android.material.datepicker.DateValidatorPointBackward
1825
import com.google.android.material.datepicker.MaterialDatePicker
1926
import com.teampophory.pophory.BuildConfig
2027
import com.teampophory.pophory.R
28+
import com.teampophory.pophory.common.bitmap.saveToDisk
2129
import com.teampophory.pophory.common.context.colorOf
2230
import com.teampophory.pophory.common.context.snackBar
2331
import com.teampophory.pophory.common.context.toast
@@ -33,6 +41,7 @@ import com.teampophory.pophory.feature.home.store.model.AlbumItem
3341
import dagger.hilt.android.AndroidEntryPoint
3442
import kotlinx.coroutines.flow.launchIn
3543
import kotlinx.coroutines.flow.onEach
44+
import kotlinx.coroutines.launch
3645
import kotlinx.datetime.Instant
3746
import java.text.SimpleDateFormat
3847
import java.util.Date
@@ -95,13 +104,53 @@ class AddPhotoActivity : AppCompatActivity() {
95104
finish()
96105
}
97106
binding.btnShare.setOnSingleClickListener {
98-
val intent = Intent("com.instagram.share.ADD_TO_STORY").apply {
99-
putExtra("source_application", BuildConfig.FACEBOOK_APP_ID)
100-
type = "image/png"
101-
putExtra("interactive_asset_uri", imageUri)
102-
putExtra("top_background_color", "#FF6724")
103-
putExtra("bottom_background_color", "#FF6724")
104-
}
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+
)
105154
}
106155
}
107156

@@ -124,7 +173,8 @@ class AddPhotoActivity : AppCompatActivity() {
124173
.setEnd(Instant.systemNow().toEpochMilliseconds()).build(),
125174
)
126175
.setSelection(
127-
currentCreatedAt + TimeZone.getDefault().getOffset(currentCreatedAt),
176+
currentCreatedAt + TimeZone.getDefault()
177+
.getOffset(currentCreatedAt),
128178
)
129179
.build()
130180
picker.show(supportFragmentManager, "datePicker")

0 commit comments

Comments
 (0)