Java Question DirAssets

derez

Expert
Licensed User
Longtime User
How can I access File.DirAssets from a jave library ?

I use the following code with Assets = True :
B4X:
if(Assets) 
    st = File.Combine(File.getDirAssets(), "WMMC.COF") ;
else
    st = File.Combine(File.getDirRootExternal() + "/Magnetics", "WMMC.COF") ;

and get the following error:
Java.io.FileNotFoundException /AssetsDir/WMMC.COF (no such file or directory)

when using False - it works (for File.DirRootExternal).
 
Last edited:

Firpas

Active Member
Licensed User
Longtime User
Hi Erel:

I have a similar problem, but I can´t find "getInputStream" method after adding "import java.io. *;"

What am I doing wrong?

Thanks in advance
 

Attachments

  • ScreenShoot.png
    277.3 KB · Views: 238

DonManfred

Expert
Licensed User
Longtime User
to create a new imputstream you should not use the iFile methods... they are available AFTER you CREATE a NEW InputStream...
Search google

B4X:
 InputStream is;
    try {
        is = new FileInputStream("file.pdf");
        // Do your work here with the is
        is.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 

Firpas

Active Member
Licensed User
Longtime User
Hi Manfred.

Thanks for your replay.

The problen is when the File is in File.DirAssets Folder:


I call library AddImage method from my App in B4A:

B4X:
Dim Jet as AGSpdf;
.....
Jet.AddImage(File.Combine(File.DirAssets, "imag0001.jpg")

And this is the library method applying your suggestion:

B4X:
public void AddImage(String FileFullPath)
]InputStream is;
    try {
        is = new FileInputStream(FileFullPath)
        // Do your work here with the is
        is.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

java.io.FileNotFoundException: /AssetsDir/imag0001.jpg: open failed: ENOENT (No such file or directory)
 

Firpas

Active Member
Licensed User
Longtime User
Ok Erel,

I`ve seen it, but i can´t find "getObject()" method.

those are my impot´s

B4X:
import java.io.*;
import java.util.*;
import com.pdfjet.*;
import android.util.Log;
import anywheresoftware.b4a.BA.*;
import anywheresoftware.b4a.objects.streams.*;
import anywheresoftware.b4a.ObjectWrapper.*;

Do I need anything else?
 

Firpas

Active Member
Licensed User
Longtime User
rHi Erel,

Thanks for your replay.

Here is my code at library:

B4X:
        public void AddImage2(String FilePath, String FileName, float x, float y, float ScaleWidth, float ScaleHeight, int ImgType){
        try {
            FileInputStream ImgFile = GetImage(FilePath, FileName);
            ...

        } catch (Exception e){
            Log.w("B4A", Log.getStackTraceString(e));
        }
    }

    private FileInputStream GetImage(String FilePath, String FileName) throws IOException {
        if(FilePath.equals(anywheresoftware.b4a.objects.streams.File.getDirAssets())) {
            if (anywheresoftware.b4a.objects.streams.File.virtualAssetsFolder != null) {
                return GetImage(anywheresoftware.b4a.objects.streams.File.virtualAssetsFolder,
                    anywheresoftware.b4a.objects.streams.File.getUnpackedVirtualAssetFile(FileName));
            } else {
                return BA.applicationContext.openFileInput(anywheresoftware.b4a.objects.streams.File.getUnpackedVirtualAssetFile(FileName));
    //            return BA.applicationContext.openFileInput(anywheresoftware.b4a.objects.streams.File.Combine(
    //                    anywheresoftware.b4a.objects.streams.File.virtualAssetsFolder,
    //                    anywheresoftware.b4a.objects.streams.File.getUnpackedVirtualAssetFile(FileName)));
            }
        } else {
            return new FileInputStream(anywheresoftware.b4a.objects.streams.File.Combine(FilePath, FileName));
        }
    }

And it is the error log:

** Activity (main) Create, isFirst = true **

** Activity (main) Resume **

java.lang.NullPointerException
at anywheresoftware.b4a.objects.streams.File.Exists(File.java:90)
at anywheresoftware.b4a.objects.streams.File.getUnpackedVirtualAssetFile(File.java:345)
at com.ags.agspdf.AGSpdf.GetImage(AGSpdf.java:233)
at com.ags.agspdf.AGSpdf.AddImage2(AGSpdf.java:222)
at b4a.example.main._createpdfjet(main.java:465)
at b4a.example.main._button1_click(main.java:380)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
at android.view.View.performClick(View.java:4660)
at android.view.View$PerformClick.run(View.java:19445)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

I've noticed that "anywheresoftware.b4a.objects.streams.File.getUnpackedVirtualAssetFile (FileName));" one who causes the exception.

Also that "anywheresoftware.b4a.objects.streams.File.virtualAssetsFolder" is null.

As you can see, the "GetImage" method must return a Java FileInputStream.

I can create this object from a string path, ...
or from a File object, ...
or from a descriptor object.

But how do I get one of these three objects?

Thank for your help.
 
Last edited:

Firpas

Active Member
Licensed User
Longtime User
Ok Erel,

this is the correct method, but returns an InputStream object and not a FileInputStream object, but I've made some changes to my code and now everything works fine.

Thanks for your help.
 
Top