Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DJgray3D · Jul 30, 2019 at 03:21 PM · androidtexturewwwruntimepersistentdatapath

Save an image to PersistentDataPath then access it again

I want to use a button to take a screenshot and save it the the PersistentDataPath on an Android device. Then on another button press, I want to get that image and apply it as a Texture.

So far, I can successfully take the screenshot and save it (with use of a plugin) but I can't seem to access it afterwards; the texture just shows a red question mark.

I've tested with pulling existing images from my Desktop and StreamingAssets, which works fine, but it seems to have an issue doing it during runtime.

When I save the image, it goes to this file path on the Android device: /storage/emulated/0/Android/data/[my bundle identifier]/files/ScreenshotApp/MyScreenshot.jpg

The script I'm using to retrieve the image from the Android Persistent Data Path:

   private IEnumerator GetImage()
     {
      string url = "file:///" + Application.persistentDataPath + "/MyScreenshot.jpg"; 
 byte[] imgData;
          UnityWebRequest www = UnityWebRequest.Get(url);
         yield return www.SendWebRequest();
         imgData = www.downloadHandler.data;
 
         if (www.error != null)
         {
             Debug.LogError(www.error);
             console.text = www.error;
             console.text += url;
             }
         print(url);
 
 Texture2D tex = new Texture2D(20, 20);
     tex.LoadImage(imgData);
 quad.GetComponent<Renderer>().material.mainTexture = tex;        
     }

The pathway that the console returns, is the same as above.

I've tried changing the prefix to the pathway to "file://", "file:///", "jar:file:///" and removing the prefix altogether but nothing seems to work.

If I use "file:///" it displays the error: Unable to read data.

Using "file://" returns the error: HTTP/1.1 404 Not Found

Using "jar:file://" returns the error: HTTP/1.1 404 Not Found

If there is no prefix it shows: Unknown Error

All of them show the red question mark.

I would appreciate anyones input on this.

Thanks in advance.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Jul 31, 2019 at 08:34 AM 0
Share

Not quite sure what you mean by the texture "showing a red question mark", but that and the nature of the error message suggests that the image isn't readable. Is that right? You've tried loading the file in something else (or directly in Unity)?

So it looks like the problem is probably in how you're saving it rather than how you're loading it. You might want to share that part of the code.

If you're using a plugin for that and don't have the code you could try contacting the author. Or not using the plugin (saving an image file isn't that complicated).

avatar image xxmariofer Bonfire-Boy · Jul 31, 2019 at 12:32 PM 0
Share

bonfire is right, maybe the image is not fully saved when you load the image, ill share some code if you end up needing to re-write the pluugin yourself

 IEnumerator Download()
 {
     WWW myWww;//change it for UnityWebRequest
 
      yield return myWww = new WWW("The path where you first import img");
      string importPath = Application.persistentDataPath+ "/Name.jpg";
 
      System.IO.File.WriteAllBytes(importPath, www.bytes);
 }
avatar image xxmariofer · Jul 31, 2019 at 08:45 AM 0
Share

add this line behind the yield return

 while(!www.isDone) { yield return new WaitForSeconds(0.2f); Debug.Log("Not finished");}
avatar image Bonfire-Boy xxmariofer · Jul 31, 2019 at 11:06 AM 0
Share

@xxmariofer Why is that necessary? They are already yielding the SendWebRequest call, doesn't that mean that we know that www is either finished or failed by the time it gets to line 7?

avatar image xxmariofer Bonfire-Boy · Jul 31, 2019 at 11:35 AM 0
Share

I am a classic guy, i continue using WWW class i have not used unitywebrequest so i am not 100% sure, and i have only used this with threads so i couldnt yield anything, with www i just create the WWW object and use the isdone property, thats why i only posted it as a comment :)

here is a code i used for basically the same task

      using (WWW www = new WWW(_url))
     {
         while(!www.isDone)
         {
             await Task.Delay(TimeSpan.FromSeconds(0.2f));//here you could change it with a yield return null; since my example was
         }
        
         // assign texture
         Texture2D texture = www.texture;

         Image image = GetComponent<Image>();
         image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
     }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dixitabhi · Sep 11, 2020 at 11:52 AM

I think the error is because your texture is unreadable. Whenever you are generating the texture/ taking the screenshot make sure the "non readable" property of the texture is set to false. If you are using a plugin, it should have a boolean parameter to set the texture readable.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

273 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loading Pictures from Android Device into game at runtime 1 Answer

red question mark loading image as texture with WWW 2 Answers

Loading an image from streamingassets fails every time on Android 1 Answer

Save Image to Device Local Storage - Appear in Phone Gallery 0 Answers

Loading 8192x8192 textures at runtime on android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges