Commit b7ee0d16 by 范玉宾

save share meditation to local commit patch

parent 6b6331d1
......@@ -19,7 +19,7 @@ import com.yidianling.muse.utils.MediaPlayerTimeUtil
import com.yidianling.muse.widget.QuitMeditationDialog
import com.yidianling.muse.widget.ShareMeditationDialog
import kotlinx.android.synthetic.main.activity_play_meditation.*
import kotlinx.android.synthetic.main.exo_player_control_view.*
import kotlinx.android.synthetic.main.player_control_view.*
import java.util.*
......
package com.yidianling.muse.utils
class Image {
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import android.view.View
import com.yidianling.common.tools.ToastUtil
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.*
import kotlin.properties.Delegates
class ShareMeditationUtil {
companion object {
fun saveImageToLocal(view: View){
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(Date())
val fileName = "IMG_$timeStamp"
val bitmap = saveViewBitmapToAlbum(view,fileName,true)
}
private fun saveViewBitmapToAlbum(view: View, name: String, isPrompt: Boolean):Bitmap?{
view.isDrawingCacheEnabled = false
view.isDrawingCacheEnabled = true
view.measure(view.measuredWidth,view.measuredHeight)
try {
val bitmap = view.drawingCache
return saveBitmapToAlbum(view.context,bitmap,name, isPrompt)
}catch (e:Exception){
}
return null
}
private fun saveBitmapToAlbum(context: Context, bitmap: Bitmap, name:String, isPrompt:Boolean):Bitmap?{
var fileName by Delegates.notNull<String>()
val galleryPath = Environment.getExternalStorageDirectory().toString()+File.separator +
Environment.DIRECTORY_DCIM + File.separator + "Camera" + File.separator
var file:File?=null
var outStream:FileOutputStream?=null
try {
file = File(galleryPath, "$name.png")
fileName = file.toString()
outStream = FileOutputStream(fileName)
bitmap.compress(Bitmap.CompressFormat.PNG,90,outStream)
}catch (e:Exception){
}finally {
try {
outStream?.close()
}catch (e:IOException){
}
}
MediaStore.Images.Media.insertImage(context.contentResolver,bitmap,fileName,null)
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
val uri = Uri.fromFile(file)
intent.data = uri
context.sendBroadcast(intent)
if (isPrompt){
ToastUtil.toastShort(if (bitmap!=null) "已保存" else "保存失败")
}
return bitmap
}
}
}
\ No newline at end of file
package com.yidianling.muse.widget
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
......@@ -12,8 +16,16 @@ import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.bumptech.glide.Glide
import com.umeng.socialize.bean.SHARE_MEDIA
import com.ydl.ydlcommon.actions.share.ShareUtils
import com.ydl.ydlcommon.utils.remind.ToastHelper
import com.yidianling.muse.R
import com.yidianling.muse.utils.ShareMeditationUtil
import kotlinx.android.synthetic.main.layout_share_play_meditation.*
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
class ShareMeditationDialog:DialogFragment() {
......@@ -56,6 +68,8 @@ class ShareMeditationDialog:DialogFragment() {
private fun initView(view: View?) {
val imageUrl = "http://static.ydlcdn.com/v1/images/logo320.png"
val shareImageUrl = arguments?.getString(KEY_SHARE_IMAGE_URL)
val shareContent = arguments?.getString(KEY_SHARE_CONTENT)
......@@ -77,23 +91,31 @@ class ShareMeditationDialog:DialogFragment() {
llQQFriend = view?.findViewById(R.id.ll_qq_friend)
llWeChat?.setOnClickListener {
ToastHelper.show("分享给微信好友!")
ShareUtils.shareTo(SHARE_MEDIA.WEIXIN,
activity as Activity, "share_title", "http://www.baidu.com", "share_context", imageUrl)
dismiss()
}
llCircleOfFriends?.setOnClickListener {
ToastHelper.show("分享到朋友圈!")
ShareUtils.shareTo(SHARE_MEDIA.WEIXIN_CIRCLE,
activity as Activity, "share_title", "https://www.baidu.com", "share_context", imageUrl)
dismiss()
}
llSave?.setOnClickListener {
ToastHelper.show("保存图片!")
ShareMeditationUtil.saveImageToLocal(csl_layout_top)
}
llQQZone?.setOnClickListener {
ToastHelper.show("分享到QQ空间!")
ShareUtils.shareTo(SHARE_MEDIA.QZONE,
activity as Activity, "share_title", "http://www.baidu.com", "share_context", imageUrl)
dismiss()
}
llQQFriend?.setOnClickListener {
ToastHelper.show("分享给QQ好友!")
ShareUtils.shareTo(SHARE_MEDIA.QQ,
activity as Activity, "share_title", "http://www.baidu.com", "share_context", imageUrl)
dismiss()
}
view?.findViewById<TextView>(R.id.tv_cancel_share)?.setOnClickListener {
......@@ -102,6 +124,41 @@ class ShareMeditationDialog:DialogFragment() {
}
private fun getBitmapFromView(view: View):Bitmap?{
val w = view.width
val h = view.height
val bitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888)
val c = Canvas(bitmap)
view.layout(0,0,w,h)
view.draw(c)
// var bitmap = Bitmap.createBitmap(view.width,view.height,Bitmap.Config.ARGB_8888)
// var canvas = Canvas(bitmap)
// canvas.drawColor(Color.WHITE)
// view.draw(canvas)
return bitmap
}
private fun savePNG(bitmap: Bitmap,name:String){
val file = File(name)
try {
val out = FileOutputStream(file)
if (bitmap.compress(Bitmap.CompressFormat.PNG,100,out)){
out.flush()
out.close()
}
}catch (e:FileNotFoundException){
}catch (e:IOException){
}
}
fun getScreenWidth() = context?.resources?.displayMetrics?.widthPixels
fun getScreenHeight() = context?.resources?.displayMetrics?.heightPixels
companion object{
const val TAG = "ShareMeditationDialog"
......
......@@ -164,6 +164,7 @@
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/csl_layout_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment