从其他应用程序(如图库应用程序图像共享到您的应用程序)接收所有数据(如图像、文本)
科特林
activity_receiving_data_from_other_app.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReceivingDataFromOtherAppActivity">
<TextView
android:id="@+id/receivingDataTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:contentDescription="@string/todo"
android:layout_marginTop="8dp"
android:id="@+id/receivingSingleImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/receivingDataTxt" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/receivingSingleImageView"
tools:ignore="SpeakableTextPresentCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewMain"
android:contentDescription="@string/todo">
</ImageView>
ImageUsPagerAdapter.kt
import android.content.Context
import android.net.Uri
import android.view.*
import androidx.viewpager.widget.PagerAdapter
import com.materialsouk.allcodeapp.R
import java.util.*
import kotlin.collections.ArrayList
class ImageUsPagerAdapter(context: Context, private val urlImage: ArrayList<String>?) :
PagerAdapter() {
private val mLayoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
override fun getCount(): Int {
return urlImage!!.size
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val itemView: View = mLayoutInflater!!.inflate(R.layout.image_item, container, false)
val imageView: ImageView= itemView.findViewById(R.id.imageViewMain)
imageView.setImageURI(Uri.parse(urlImage!![position]))
Objects.requireNonNull(container).addView(itemView)
return itemView
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View?)
}
}
ReceivingDataFromOtherAppActivity.kt
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
class ReceivingDataFromOtherAppActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receiving_data_from_other_app)
when {
intent?.action == Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
handleSendText(intent) // Handle text being sent
} else if (intent.type?.startsWith("image/") == true) {
handleSendImage(intent) // Handle single image being sent
}
}
intent?.action == Intent.ACTION_SEND_MULTIPLE
&& intent.type?.startsWith("image/") == true -> {
handleSendMultipleImages(intent) // Handle multiple images being sent
}
}
}
private fun handleSendText(intent: Intent) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
findViewById<TextView>(R.id.receivingDataTxt).text = it
}
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
// Update UI to reflect image being shared
findViewById<ImageView>(R.id.receivingSingleImageView).setImageURI(it)
}
}
private fun handleSendMultipleImages(intent: Intent) {
intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
// Update UI to reflect multiple images being shared
val mViewPager = findViewById<ViewPager>(R.id.viewPager)
val imageArrayList : ArrayList<String> = ArrayList()
for (i in it){
imageArrayList.add(i.toString())
}
val mViewPagerAdapter = ImageUsPagerAdapter(this, imageArrayList)
mViewPager.adapter = mViewPagerAdapter
}
}
}
AndroidManifest.xml
<application ...>
<activity
android:name=".ReceivingDataFromOtherAppActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
</application>