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: 348

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:


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.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…