LibGdx load Texture from SmbFile

83 Views Asked by At

I try to create a Gdx Texture with an image file located in a shared folder on my PC. The code works well on desktop app (but it works as well without using SmbFile...), but crash on android app. I obtain "no such file or directory" error. Does somebody knows how can we do that ? Thank's !

        public void create () {
        Gdx.app.setLogLevel(Application.LOG_DEBUG);

        batch = new SpriteBatch();

        SmbFile file=null;

        try {
            file = new SmbFile("smb://***path to shared folder***/icon-152.png");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FileHandle fileHandle;
        fileHandle = new FileHandle(file.getUncPath());

        img = new Texture(fileHandle); //***No such file or directory***
        //img = new Texture(Gdx.files.external(file.getUncPath())); //***No such file or directory***
    }

Add on : I tried to copy the File in assets before to load it as a Texture. Still working fine on desktop app, but Stil having an error on android app : Java.io.FiliNotFound Exception.

    public void create () {
        Gdx.app.setLogLevel(Application.LOG_DEBUG);

        batch = new SpriteBatch();

        SmbFile file=null;

        try {
            file = new SmbFile("smb://***path to shared folder***/icon-152.png");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(file.getUncPath());
            os = new FileOutputStream("test.png");
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            is.close();
            os.close();
        }
        catch(java.io.IOException e){
            Gdx.app.log("", e.getMessage()); //***Java.io.FileNotFound Exception***
        }

        img = new Texture("test.png"); //***No such file or directory***

    }
1

There are 1 best solutions below

0
On

Yes Nicolas,

I finaly did it by first copying the file in local. It is working on both android and desktop app. If somebody is interested, the two functions loadFile and saveFile :


        loadFile( "//***path to shared folder***/icon-152.png","icon-152.png");

        saveFile("icon-152.png", "//HP2285/***path to shared folder***/icon-152.png");

    }

    public void loadFile(String smbFilePath, String fileName){

        try {
            SmbFile file = new SmbFile("smb:"+smbFilePath);
            InputStream is = new SmbFileInputStream(file);

            FileHandle fhd = Gdx.files.local(fileName);
            OutputStream os = fhd.write(false);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }

            is.close();
            os.close();
        }
        catch(Exception e){
        }
    }

    public void saveFile(String fileName, String smbFilePath){

        try {
            SmbFile file = new SmbFile("smb:"+smbFilePath);
            OutputStream os = new SmbFileOutputStream(file);

            FileHandle fhs = Gdx.files.local(fileName);
            InputStream is = fhs.read();

            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read (buffer)) > 0) {
                os.write(buffer, 0, length);
            }

            is.close();
            os.close();
        }
        catch(Exception e){
        }

    }