質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

3597閲覧

UnityでAndroidへのファイルコピーの方法

designer_s_egg

総合スコア1

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/01/20 03:47

前提・実現したいこと

unityでdbを利用したAndroidアプリを作成したいのですが、streamingAssetsからAndroidにコピーするところで失敗しているようです。
エディタ上では正常に動作するのですが、端末上だとうまく動かず困っています。どうすれば解決するのかをご教示願いたいです。

発生している問題・エラーメッセージ

FileNotFoundException: jar:file:///data/app/~~CvmWIkmT4orzuZ7qTwS1sw==/com.DefaultCompany.BookLibraly-XVa2qiXUCxDC9hXop_YO5g==/base.apk!/assets/bookList.db does not exist

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.IO; 5 6public class CopyFile : MonoBehaviour 7{ 8 void Start(){ 9 string tempFilePath = Application.persistentDataPath + "/bookList.db"; 10 11 #if UNITY_EDITOR 12 string baseFilePath = Application.streamingAssetsPath + "/bookList.db"; 13 #elif UNITY_ANDROID 14 string baseFilePath = "jar:file://" + Application.dataPath + "!/assets/bookList.db"; 15 #endif 16 17 Debug. Log("FilePath = " + Application.persistentDataPath); 18 FileCopy(baseFilePath, tempFilePath); 19 } 20 static void FileCopy(string baseFilePath, string tempFilePath){ 21 if(System.IO.File.Exists(tempFilePath) == false){ 22 Debug.Log("did not copy file"); 23 File.Copy(baseFilePath, tempFilePath); 24 } 25 } 26} 27

試したこと

ファイルパスをApplication.streamingAssetsPath + "/bookList.dbからjar:file://" + Application.dataPath + "!/assets/bookList.dbのように編集

補足情報(FW/ツールのバージョンなど)

・Unity2019.4.16F1
・Android 11

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Android端末の場合、File.Copy()を含め、StreamingAssetsに対しての基本的なファイル操作は行えません。
以下のように、UnityWebRequestを使ってください。
(動作未確認なので、上手くいかなかったら直してください)

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.IO; 5using UnityEngine.Networking; 6 7public class CopyFile : MonoBehaviour 8{ 9 void Start() 10 { 11 string tempFilePath = Application.persistentDataPath + "/bookList.db"; 12 13 string baseFilePath = Application.streamingAssetsPath + "/bookList.db"; 14 15 Debug.Log("FilePath = " + Application.persistentDataPath); 16 FileCopy(baseFilePath, tempFilePath); 17 } 18 19 void FileCopy(string baseFilePath, string tempFilePath) 20 { 21 if (System.IO.File.Exists(tempFilePath) == false) 22 { 23 if (baseFilePath.Contains("://")) 24 { 25 // Androidの場合 26 StartCoroutine(FileCopyForAndroid(baseFilePath, tempFilePath)); 27 } 28 else 29 { 30 File.Copy(baseFilePath, tempFilePath); 31 } 32 } 33 } 34 35 IEnumerator FileCopyForAndroid(string baseFilePath, string tempFilePath) 36 { 37 using (var webRequest = UnityWebRequest.Get(baseFilePath)) 38 { 39 yield return webRequest.SendWebRequest(); 40 41 if (webRequest.isNetworkError || webRequest.isHttpError) 42 { 43 // エラー処理 44 45 yield break; 46 } 47 48 File.WriteAllBytes(tempFilePath, webRequest.downloadHandler.data); 49 } 50 } 51}

投稿2021/01/20 04:12

編集2021/01/20 04:18
fiveHundred

総合スコア9805

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

designer_s_egg

2021/01/21 02:58 編集

ありがとうございます! 掲載していたエラーはなくなり、コピーできました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問