loading a string from a raw resource file in android

69 views
Skip to first unread message

David Liebman

unread,
Aug 16, 2015, 2:11:15 PM8/16/15
to Py4A
HI,

I have an android class that looks like this:

----------------------------------------

package org.renpy.android;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import org.renpy.android.ResourceManager;


public class GetText {

    Context context;

    public ResourceManager mManager;

    public void GetText() {


    }


    public String getText(Activity c, String in) {

        mManager = new ResourceManager(c);
        int mId = mManager.getIdentifier(in,"raw");

        context = c.getApplicationContext();

        String out = "";

        if (true) {

            InputStream inputStream = context.getResources().openRawResource(mId);

            InputStreamReader inputreader = new InputStreamReader(inputStream);
            BufferedReader buffreader = new BufferedReader(inputreader);
            String line;
            StringBuilder text = new StringBuilder();

            try {
                while ((line = buffreader.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
            } catch (IOException e) {
                return null;
            }
            //return text.toString();
            out = text.toString();
        }
        return out;
    }
}

-----------------------------------------

I also have some python code that looks like this:

-----------------------------------------

i=0
GetText = jnius.autoclass("org.renpy.android.GetText")
PythonActivity = jnius.autoclass('org.renpy.android.PythonActivity')

loader = GetText()

shapew1 = str(name+'_shape_w'+str(i+1))

activity = PythonActivity.mActivity

wshape = loader.getText(activity, shapew1)

-----------------------------------------

BUT, my android app always crashes on the line that starts 'wshape = loader...'

what am i doing wrong?? loader.getText() is supposed to return a string to wshape, from a resource that I know exists. Is this sort of thing not possible with pyjnius?

thanks,
Dave Liebman

David Liebman

unread,
Aug 21, 2015, 9:14:06 AM8/21/15
to Py4A
I got this to work. my problem was that I constructed the name string wrong. I don't show it much in the code that I listed, but there is a variable 'shapew1' and it must be made to match the raw resource that you are looking to read. My mistake was that the name that I was using didn't match the name of the resources.
Reply all
Reply to author
Forward
0 new messages