Android System Picture Sharing Tool Class

  • 2021-11-01 04:52:27
  • OfStack

Brief introduction

Record a tool class that uses the system sharing function to share pictures (the code is written with Kotlin, which is a relatively simple syntax, and some places that may need to be customized have been marked). The invocation method is relatively simple:


Util.startShareImage(this) //this For the current Activity Instances 

Authority

Remember to add file operation rights, and pay attention to the rights management above version 6.0


 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

See the code for details


/**
 *  System sharing picture function 
 * Created by wiky on 2018/1/13.
 */

object Util {

 fun startShareImage(activity: Activity) {
  // Filter out the corresponding platforms that need to be shared: WeChat friends, friends circle, QQ Friends.   Self-modifiable 
  val targetApp = arrayOf("com.tencent.mm.ui.tools.ShareImgUI", "com.tencent.mm.ui.tools.ShareToTimeLineUI", "com.tencent.mobileqq.activity.JumpActivity")
  /** *  Share pictures  */
  val bitmap = getImageFromAssetsFile(activity, "img_share.jpg") // From assets Fetch the corresponding file from the directory, and modify the file name by yourself 
  val localImage = saveBitmap(bitmap!!, "share.jpg") // Before sharing, you need to save the picture locally (remember to add permission) and modify the file name by yourself 
  val shareIntent = Intent(Intent.ACTION_SEND)
  shareIntent.type = "image/*" // Set the type of shared content: Picture 
  shareIntent.putExtra(Intent.EXTRA_STREAM, localImage)
  try {
   val resInfo = activity.packageManager.queryIntentActivities(shareIntent, 0)
   if (!resInfo.isEmpty()) {
    val targetedShareIntents = ArrayList<Intent>()
    for (info in resInfo) {
     val targeted = Intent(Intent.ACTION_SEND)
     targeted.type = "image/*" // Set the type of shared content 
     val activityInfo = info.activityInfo
     // If you need to share it with other platforms, you can print out the specific information and find the corresponding one Activity Name, just fill in the array above 
//     println("package = ${activityInfo.packageName}, activity = ${activityInfo.name}")

     // Filter (show only the platforms to share) 
     if (targetApp.any { it == activityInfo.name }) {
      val comp = ComponentName(activityInfo.packageName, activityInfo.name)
      targeted.component = comp
      targeted.putExtra(Intent.EXTRA_STREAM, localImage)
      targetedShareIntents.add(targeted)
     }
    }
    val chooserIntent = Intent.createChooser(targetedShareIntents.removeAt(0), " Select the platform to share to ")
    if (chooserIntent != null) {
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toTypedArray<Parcelable>())
     activity.startActivity(chooserIntent)
    }
   }
  } catch (e: Exception) {
   Log.e(StatConstants.LOG_TAG, "Unable to share image, logs : " + e.toString())
  }
 }

 /** *  From Assets Read pictures from  */
 private fun getImageFromAssetsFile(context: Context, fileName: String): Bitmap? {
  var image: Bitmap? = null
  val am = context.resources.assets
  try {
   val inputStream = am.open(fileName)
   image = BitmapFactory.decodeStream(inputStream)
   inputStream.close()
  } catch (e: IOException) {
   e.printStackTrace()
  }

  return image
 }

 /** *  Save pictures locally  */
 private fun saveBitmap(bm: Bitmap, picName: String): Uri? {
  try {
   val dir = Environment.getExternalStorageDirectory().absolutePath + File.separator + picName
   val f = File(dir)
   if (!f.exists()) {
    f.parentFile.mkdirs()
    f.createNewFile()
   }
   val out = FileOutputStream(f)
   bm.compress(Bitmap.CompressFormat.JPEG, 90, out)
   out.flush()
   out.close()
   return Uri.fromFile(f)
  } catch (e: FileNotFoundException) {
   e.printStackTrace()
  } catch (e: IOException) {
   e.printStackTrace()
  }

  return null
 }

}

Related articles: