日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Unity保存图片到相册

發(fā)布時(shí)間:2024/1/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity保存图片到相册 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Unity保存圖片到Android相冊(cè)

Java]?純文本查看?復(fù)制代碼

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

package com.xuefei.game;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import com.unity3d.player.UnityPlayerNativeActivity;

import android.content.Context;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.Bitmap.CompressFormat;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.widget.Toast;

public class MainActivity extends UnityPlayerNativeActivity {

????public static Context context;

????public static MainActivity mainActivity;

????@Override

????protected void onCreate(Bundle savedInstanceState) {

????????super.onCreate(savedInstanceState);

????????mainActivity = this;

????}

????// 保存到相冊(cè)

????public static void savePng(final String fileName) {

????????context = mainActivity.getApplicationContext();

????????mainActivity.runOnUiThread(new Runnable() {

????????????public void run() {

????????????????Bitmap bitmap = BitmapFactory.decodeFile(Environment

????????????????????????.getExternalStorageDirectory()

????????????????????????+ "/Android/data/com.xuefei.game/files/"

????????????????????????+ fileName

????????????????????????+ ".png");

????????????????File file = new File(Environment.getExternalStorageDirectory()

????????????????????????+ "/DCIM/Camera", fileName + ".jpg");

????????????????FileOutputStream fos = null;

????????????????try {

????????????????????fos = new FileOutputStream(file);

????????????????} catch (FileNotFoundException e) {

????????????????????// TODO Auto-generated catch block

????????????????????Log.w("cat", e.toString());

????????????????}

????????????????bitmap.compress(CompressFormat.JPEG, 100, fos);

????????????????try {

????????????????????fos.flush();

????????????????} catch (IOException e) {

????????????????????// TODO Auto-generated catch block

????????????????????Log.w("cat", e.toString());

????????????????}

????????????????try {

????????????????????fos.close();

????????????????} catch (IOException e) {

????????????????????// TODO Auto-generated catch block

????????????????????Log.w("cat", e.toString());

????????????????}

????????????????bitmap.recycle();//掃描保存的圖片

????????????????context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" +Environment.getExternalStorageDirectory()

????????????????????????+ "/DCIM/Camera/"+fileName + ".jpg")));

?????????????????

????????????????Toast.makeText(context, "照片已保存到相冊(cè)", Toast.LENGTH_SHORT).show();

??

????????????}

????????});

????}

}



Unity中代碼:

[C#]?純文本查看?復(fù)制代碼

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

if (GUI.Button(new Rect(0, 720, 400, 120), "SavePng"))

????????{

????????????StartCoroutine(SavePngToSD(DateTime.Now.ToFileTime().ToString()));

????????}

/// <summary>

????/// 截圖后先檢測(cè)圖片是否保存成功然后調(diào)用保存到相冊(cè)

????/// </summary>

????/// <param name="name"></param>

????public void SavePng(string name)

????{

#if UNITY_ANDROID

????????AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

????????AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

????????jo.CallStatic("SavePng", name);

#elif? UNITY_IPHONE

#endif

????}

/// <summary>

????/// 保存截圖到相冊(cè)

????/// </summary>

????/// <param name="pngName"></param>

????/// <returns></returns>

????IEnumerator SavePngToSD(string pngName)

????{

????????yield return new WaitForEndOfFrame();

????????Application.CaptureScreenshot(pngName + ".png");

????????while (!IsFileExistByPath(Application.persistentDataPath + "/" + pngName + ".png"))

????????{

????????????yield return new WaitForSeconds(0.05f);

????????}

????????SavePng(pngName);

????????yield return new WaitForSeconds(0.3f);

????????//測(cè)試請(qǐng)求文件

????????WWW www = new WWW("file:///" + Application.persistentDataPath + "/" + pngName + ".png");

????????yield return www;

????}

????/// <summary>

????/// 檢測(cè)文件是否存在

????/// </summary>

????/// <param name="path"></param>

????/// <returns></returns>

????public static bool IsFileExistByPath(string path)

????{

????????FileInfo info = new FileInfo(path);

????????bool b = false;

????????if (info == null || info.Exists == false)

????????{

????????????b = false;

????????}

????????else

????????{

????????????b = true;

????????}

????????return b;

????}


注意文件路徑是否存在,不存在就創(chuàng)建


流程:Unity截屏--調(diào)用安卓拷貝到相冊(cè)--掃描保存的文件

解決的問題:截屏圖片在相冊(cè)不顯示。

Unity截屏插入iOS相冊(cè)?

[C#]?純文本查看?復(fù)制代碼

?

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

using System;

using System.IO;

?

#if UNITY_IOS

using System.Runtime.InteropServices;

#endif

?

using UnityEngine;

?

public class UnityPlugins : MonoBehaviour

{

?

????// Use this for initialization

????void Start()

????{

?

????}

?

????// Update is called once per frame

????void Update()

????{

?

????}

?

????#if UNITY_IOS

????[DllImport("__Internal")]

????private static extern void _SavePhoto(string readAddr);

????#endif

?

????string path = "";

?

????private void OnGUI()

????{

?

????????if (GUILayout.Button("TakePhoto", GUILayout.Height(300), GUILayout.Width(300)))

????????{

????????????CaptureCamera();

????????????Debug.Log("TakePhoto");

????????}?

????}

?

?

????void CaptureCamera()

????{

????????Camera camera = Camera.main;

????????string name = DateTime.Now.ToFileTime().ToString();

?

????????Rect rect = new Rect(0, 0, Screen.width, Screen.height);

?

????????RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 0);

????????Texture2D frame = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);

?

????????camera.targetTexture = rt;

????????camera.Render();

?

????????RenderTexture.active = rt;

????????frame.ReadPixels(rect, 0, 0);

????????frame.Apply();

?

????????camera.targetTexture = null;?

????????RenderTexture.active = null;??

?

????????byte [] bytes = frame.EncodeToJPG();

?

????????File.WriteAllBytes(Application.persistentDataPath + "/" + name + ".jpg",bytes);

?

????????#if UNITY_IOS

????????_SavePhoto(Application.persistentDataPath + "/" + name + ".jpg");

????????#endif

????}

?

}




iOS代碼

[AppleScript]?純文本查看?復(fù)制代碼

?

?

1

2

3

4

5

#import <Foundation/Foundation.h>?

????

@interface UnityPlugins : NSObject?

- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error??

????contextInfo: ( void *) contextInfo;?

@end



?

[AppleScript]?純文本查看?復(fù)制代碼

?

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

#import "UnityPlugins.h"?

@implementation UnityPlugins?

- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error??

????contextInfo: ( void *) contextInfo?

{?

????NSLog(@"保存結(jié)束");?

????if (error != nil) {?

????????NSLog(@"有錯(cuò)誤");?

????}?

}?

void _SavePhoto(char *readAddr)?

{?

????NSString *strReadAddr = [NSString stringWithUTF8String:readAddr];?

????UIImage *img = [UIImage imageWithContentsOfFile:strReadAddr];?

????NSLog([NSString stringWithFormat:@"w:%f, h:%f", img.size.width, img.size.height]);?

????UnityPlugins *instance = [UnityPlugins alloc];?

????UIImageWriteToSavedPhotosAlbum(img, instance,??

????????@selector(imageSaved:didFinishSavingWithError:contextInfo:), nil);?

}?

@end




目錄結(jié)構(gòu)
?


導(dǎo)出Xcode工程后或者??修改Info.plist??打開 Info.plist,點(diǎn)擊 + 號(hào),在 Key 中輸入:Privacy - Photo Library Additions Usage Description,Type 選擇 String,Value 中輸入你的提示語(yǔ)(比如XXX訪問相冊(cè))再次 Build

總結(jié)

以上是生活随笔為你收集整理的Unity保存图片到相册的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。