Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android應用程序中分享圖片和文字給好友

Android應用程序中分享圖片和文字給好友

編輯:關於Android編程

 


配置文件中需要添加讀寫sdcard的權限

 

using UnityEngine;
using System.Collections;
using System.IO;
     
public class Share : MonoBehaviour
{
         
    public  static string imagePath;
    static AndroidJavaClass sharePluginClass;
    static AndroidJavaClass unityPlayer;
    static AndroidJavaObject currActivity;
    private static Share mInstance;
         
    public static Share instance {
        get{ return mInstance;}
    }
         
    void Awake ()
    {
        mInstance = this;
    }
         
    void Start ()
    {
        imagePath = Application.persistentDataPath + /HKeyGame.png;
        sharePluginClass = new AndroidJavaClass (com.ari.tool.UnityAndroidTool);
        if (sharePluginClass == null) {
            Debug.Log (sharePluginClass is null);
        } else {
            Debug.Log (sharePluginClass is not null);
        }
        unityPlayer = new AndroidJavaClass (com.unity3d.player.UnityPlayer);
        currActivity = unityPlayer.GetStatic (currentActivity);
    }
         
    public void CallShare (string handline, string subject, string text, bool image)
    {
        Debug.Log (share call start :  + imagePath);
        if (image) {
            sharePluginClass.CallStatic (share, new object[] {
                handline,
                subject,
                text,
                imagePath
            });
        } else {
            sharePluginClass.CallStatic (share, new object[] {
                handline,
                subject,
                text,
                
            });
        }
        Debug.Log (share call end);
    }
         
    public void ScreenShot ()
    {
        StartCoroutine (GetCapture ());
    }
         
    IEnumerator GetCapture ()
    {
             
        yield return new WaitForEndOfFrame ();
             
        int width = Screen.width;
             
        int height = Screen.height;
             
        Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
             
        tex.ReadPixels (new Rect (0, 0, width, height), 0, 0, true);
             
        byte[] imagebytes = tex.EncodeToPNG ();//轉化為png圖
             
        tex.Compress (false);//對屏幕緩存進行壓縮
             
        //      image.mainTexture = tex;//對屏幕緩存進行顯示(縮略圖)
             
        File.WriteAllBytes (Application.persistentDataPath + /HKeyGame.png, imagebytes);//存儲png圖
             
        Debug.Log (Application.persistentDataPath);
    }
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved