Java Question DirAssets

Discussion in 'Libraries developers questions' started by derez, May 9, 2012.

  1. derez

    derez Expert Licensed User

    How can I access File.DirAssets from a jave library ?

    I use the following code with Assets = True :
    Code:
    if(Assets) 
        st = 
    File.Combine(File.getDirAssets(), "WMMC.COF") ;
    else
        st = 
    File.Combine(File.getDirRootExternal() + "/Magnetics""WMMC.COF") ;
    and get the following error:
    when using False - it works (for File.DirRootExternal).
     
    Last edited: May 9, 2012
  2. Erel

    Erel Administrator Staff Member Licensed User

    The assets folder is not a real folder. You cannot treat it as a standard folder. The various File methods know how to treat this folder.
    The simplest solution is to call File.getInputStream. This method will open correctly all files.
     
  3. Firpas

    Firpas Active Member Licensed 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
     

    Attached Files:

  4. DonManfred

    DonManfred Expert Licensed User

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

    Code:
    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();
        }
     
  5. Firpas

    Firpas Active Member Licensed 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:

    Code:
    Dim Jet as AGSpdf;
    .....
    Jet.AddImage(
    File.Combine(File.DirAssets, "imag0001.jpg")
    And this is the library method applying your suggestion:

    Code:
    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();
        }
    }
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. Firpas

    Firpas Active Member Licensed User

    Ok Erel,

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

    those are my impot´s

    Code:
    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?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Read the post carefully. It is simpler. The code posted is only for the rare case where you need to work with file descriptors.
     
  9. Firpas

    Firpas Active Member Licensed User

    rHi Erel,

    Thanks for your replay.

    Here is my code at library:

    Code:
    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: Jan 28, 2015
  10. Erel

    Erel Administrator Staff Member Licensed User

    You don't need to use this code. You just need to call anywheresoftware.b4a.objects.streams.File.OpenInput
     
  11. Firpas

    Firpas Active Member Licensed 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.
     

Share This Page

Loading...