Newer
Older
MonCalc / app / src / main / kotlin / jackall / moncalc / utils / ShareUtil.kt
package jackall.moncalc.utils

import android.app.Activity
import android.graphics.Bitmap
import android.net.Uri
import android.support.v4.app.ShareCompat
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


/**
 * Created by matsumoto_k on 2017/11/07.
 */
class ShareUtil {
    companion object {
        fun shareImageAndText(activity: Activity, bitmap: Bitmap, title: String, subject: String, text: String) {
            val builder = ShareCompat.IntentBuilder.from(activity)
            builder.setChooserTitle(title)
            builder.setSubject(subject)
            builder.setText(text)

            val cache = activity.externalCacheDir
            val sharefile = File(cache, "toshare.png")
            try {
                val out = FileOutputStream(sharefile)
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
                out.flush()
                out.close()
            } catch (e: IOException) {

            }

            val image = Uri.parse("file://" + sharefile)

            builder.setStream(image)
            builder.setType("image/jpeg")
            builder.startChooser()
        }
    }
}