Dia
Dia

Reputation: 65

Can not resolve getAssets() method from inside a java file: Android

I have created a file inside assests folder and now I want to read the file from a java class and pass it to another function in the same class but for some reason i am unable to use getAssest() method. Please help!

    public void configuration()
        {
            String text = "";
            try {
                InputStream is = getAssets().open("config.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                text = new String(buffer);


            } catch (IOException e) {
                e.printStackTrace();
            }


        }

public IExtraFeeCalculator getExtraFeeCalculator()
    {
        if(efCalculator==null)
        {
            if(configuration(Context context) == "extrafeeCalculaotor")
            {
                String className = System.getProperty("extraFeeCalculator.class.name");
                try {
                    efCalculator = (IExtraFeeCalculator)Class.forName(className).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }

        }
        return efCalculator;
    }

Upvotes: 0

Views: 3796

Answers (3)

9spl
9spl

Reputation: 357

Change your Method with Single Parameter Context .... Pass Context from where you Call this Method..

public void configuration(Context context)
    {
        String text = "";
        try {
            InputStream is = context.getAssets().open("config.txt");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            text = new String(buffer);


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

Yes now as per i think you are not aware from java structure...

Suppose you have this YOUR_CLASS_NAME.java

public void YOUR_CLASS_NAME{

Context context;

YOUR_CLASS_NAME(Context context){

           this.context=context;
}

public void configuration(Context context)
        {
            String text = "";
            try {
                InputStream is = getAssets().open("config.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                text = new String(buffer);


            } catch (IOException e) {
                e.printStackTrace();
            }


        }

public IExtraFeeCalculator getExtraFeeCalculator()
    {
        if(efCalculator==null)
        {
            if(configuration(context) == "extrafeeCalculaotor")
            {
                String className = System.getProperty("extraFeeCalculator.class.name");
                try {
                    efCalculator = (IExtraFeeCalculator)Class.forName(className).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }

        }
        return efCalculator;
    }

}

Upvotes: 1

Ghanshyam Sharma
Ghanshyam Sharma

Reputation: 451

You should try

getResources().getAssets().open("config.txt")

instead of

context.getAssets().open("config.txt");

Upvotes: 1

Santy
Santy

Reputation: 24

Use this Code

BufferedReader reader = null;
try {
 StringBuilder returnString = new StringBuilder();
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
        returnString.append(mLine );

    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

Upvotes: 0

Related Questions