How to save byte array as a file in android and how to view that file in codenameone?

1,756 views
Skip to first unread message

kantha...@gmail.com

unread,
Feb 12, 2018, 1:22:17 AM2/12/18
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

I am trying to save the byte array as a file using below code.

FileSystemStorage fs = FileSystemStorage.getInstance();
       
OutputStream os = fs.openOutputStream(fs.getAppHomePath() + "MyFileName.txt");
        os
.write(myByteData);
       
Util.cleanup(os);


I have tested this in android 7.0 and android 4.4.. 

In android 7.0, fs.getAppHomePath() returning path like file:///data/user/0/com.engravsystems.emqim/files/

In android 4.4, fs.getAppHomePath() returning path like file:///data/data/com.engravsystems.emqim/files/ 

and i am getting exception like "java.io.IOException: /com.engravsystems.emqim/files/MyFileName.pdf not found" in both devices.


Is there something I'm missing? 



Shai Almog

unread,
Feb 13, 2018, 12:15:10 AM2/13/18
to CodenameOne Discussions
I think your code is correct. I think that app home might not exist which seems to me like an omission on our part.
Try

fs.mkdir(fs.getAppHomePath());

kantha...@gmail.com

unread,
Feb 13, 2018, 4:09:04 AM2/13/18
to CodenameOne Discussions
Thanks shai for reply.. I have tried "fs.mkdir(fs.getAppHomePath());" like below but still i'm getting  
java.io.IOException: file:///data/data/com.engravsystems.emqim/files//MyFileName.pdf not found ..

here is the code :

  FileSystemStorage fs = FileSystemStorage.getInstance();
                    try {

if(!fs.exists(fs.getAppHomePath()))
fs.mkdir(fs.getAppHomePath());

OutputStream os = fs.openOutputStream(fs.getAppHomePath() + "MyFileName.pdf");
os.write(hrFiles.get(finalI).get("fileData").toString().getBytes());
Util.cleanup(os);
} catch (IOException e) {
e.printStackTrace();
}

Steve Hannah

unread,
Feb 13, 2018, 7:34:12 AM2/13/18
to codenameone...@googlegroups.com
It is very strange that the IOException is reporting the path with the file:/// prefix.   This should have been stripped by the time it gets there.  Can you post a full stack trace?

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/546eb817-a1d9-486b-a507-28d94a4a1f86%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

kantha...@gmail.com

unread,
Feb 14, 2018, 12:02:03 AM2/14/18
to CodenameOne Discussions
Sorry for late reply Steve..
here is the full stack trace

Resource not found: file:///data/data/com.mypackage.myapp/files/MyFileName.pdf
java.io.IOException: file:///data/data/com.mypackage.myapp/files/MyFileName.pdf not found
     at com.codename1.ui.util.Resources.open(Resources.java:740)
     at com.codename1.ui.util.Resources.open(Resources.java:679)
     at com.mypackage.myapp.MyFileNameForm$1.actionPerformed(MyFileNameForm.java:158)
     at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
     at com.codename1.ui.Button.fireActionEvent(Button.java:499)
     at com.codename1.ui.Button.released(Button.java:533)
     at com.codename1.ui.Button.pointerReleased(Button.java:637)
     at com.codename1.ui.Form.pointerReleased(Form.java:2980)
     at com.codename1.ui.Component.pointerReleased(Component.java:4100)
     at com.codename1.ui.Display.handleEvent(Display.java:2061)
     at com.codename1.ui.Display.edtLoopImpl(Display.java:1043)
     at com.codename1.ui.Display.mainEDTLoop(Display.java:961)
     at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
     at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
     at java.lang.Thread.run(Thread.java:841)

and i forgot to mention that , while trying to open file using below code getting this exception.. 

try {
Resources.open(fs.getAppHomePath() + "MyFileName.pdf");
} catch (IOException e) {
e.printStackTrace();
}

but still don't know it is saving or not..

Shai Almog

unread,
Feb 14, 2018, 12:28:41 AM2/14/18
to CodenameOne Discussions
Why are you calling Resources.open?
Try Display.getInstance().execute().

Resources.open works with "resource files" not files.

kantha...@gmail.com

unread,
Feb 14, 2018, 6:35:20 AM2/14/18
to CodenameOne Discussions
when i tried to call "Display.getInstance().execute()" method it is asking URL but here i have byte array(byte[])..
so how can i use "Display.getInstance().execute()" for byte array(byte[]).

Steve Hannah

unread,
Feb 14, 2018, 8:51:55 AM2/14/18
to codenameone...@googlegroups.com
If you're loading a resource file from the file system, you'll need to use the Resources.open(InputStream) variant.  (i.e. open an input stream from the file system, and pass that to Resources.open().

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.

kantha...@gmail.com

unread,
Feb 14, 2018, 12:59:35 PM2/14/18
to CodenameOne Discussions
No Steve, i don't want to load resource file,  here i want to load byte array(getting from service) as pdf file.

how to load byte array as pdf file? .    

Shai Almog

unread,
Feb 15, 2018, 12:23:39 AM2/15/18
to CodenameOne Discussions
You did 99% of the work. You saved the file to the right location now you just need to execute that file:

Display.getInstance().execute(fs.getAppHomePath() + "MyFileName.pdf");

kantha...@gmail.com

unread,
Feb 16, 2018, 8:16:48 AM2/16/18
to CodenameOne Discussions
Thank you so much shai.. its working now .. 
Reply all
Reply to author
Forward
0 new messages