0

R.raw.content1_1 is a int. there are a text to store all the content's path. when I use the buffered reader to read the path , it cannot work. But if I don't parse String to int, it do well. when i want to display the content, i just find the path is string "R.raw.content1_1" i get from database.

public void addContent(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                BufferedReader localBufferReader = new BufferedReader(
                        new InputStreamReader(getResources().openRawResource(
                                R.raw.title1)));
                BufferedReader localBufferReader1 = new BufferedReader(
                        new InputStreamReader(getResources().openRawResource(
                                R.raw.content_path1)));
                BufferedReader localBufferReader2 = new BufferedReader(
                        new InputStreamReader(getResources().openRawResource(
                                R.raw.video_path1)));

                for (int i = 1; i < 97; i++) {
                    String title = localBufferReader.readLine();
                   // int content_path =Integer.parseInt(localBufferReader1.readLine());
                    String content_path1 = localBufferReader1.readLine();
                    //the content_path1 cannot parse to int.
                    int content_path = Integer.parseInt(content_path1);
                    String video_path = localBufferReader2.readLine();
                    if (title == null) {
                        localBufferReader.close();
                        localBufferReader1.close();
                        localBufferReader2.close();
                        break;
                    }

                    ContentValues cv = new ContentValues();
                    cv.put("id", i);
                    cv.put("title", title);
                    cv.put("content", content_path);
                    cv.put("path", video_path);
                    db.insert("book1", null, cv);
                }




                Log.v("successful","777hhaa");
                db.close();
            }catch (Exception e){
                e.printStackTrace();
            }
}

if i store the path is string in database, openRawResource(int)...cant open the raw resource

private void displayContent(){
    String course = mainActivity.getCourse();
    book = mainActivity.getBook();
    String columns = "content";
    Cursor cursor = db.query(book, new String[] {columns}, "title=?", new String[] {course}, null, null, null, null);
    while (cursor.moveToNext()) {
        cursor.getString(cursor.getColumnIndex("content"));
        content = cursor.getString(0);
        //content = Integer.parseInt(content1);

        int content1 = Integer.parseInt(content,16);

        textView.setText(content1);
    }
    cursor.close();
 /*
    try{
        InputStream inputStream = getResources().openRawResource(content);

        InputStreamReader inputReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputReader);

        textView.setText(bufferedReader.readLine());

    }catch (Exception e){
        e.printStackTrace();
    }*/



}

all the error message :

 AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.iuuu.listenstudy, PID: 2721
    java.lang.NumberFormatException: Invalid int: "R.raw.content1_1"
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parse(Integer.java:410)
        at java.lang.Integer.parseInt(Integer.java:367)
        at com.example.iuuu.listenstudy.ContentFragment.addContent(ContentFragment.java:58)
        at com.example.iuuu.listenstudy.ContentFragment.onCreateView(ContentFragment.java:42)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
zhen She
  • 129
  • 1
  • 9

2 Answers2

0

R.raw.content1_1, might be an int, but "R.raw.content1_1" surely isn't and there is no way to parse it to say it's resource 1 or resource n. you should store the resource id in the raw file not the 'resource name'.

you could use assets instead of what you did there to save the full path to the resource and access it by ... path rather than id

Nicolae Natea
  • 1,185
  • 9
  • 14
0

The issue is that you're trying to parse R.raw.content1_1 as an int where what I think you want to do is open whatever resource R.raw.content1_1 is pointing to and parse that as an int.

Since you're getting the path to the raw resource from a file you can't use the same syntax as you do in code since you'll be getting a String containint R.raw.content1_1 instead of Java Code that points to the resource ID.

Instead you should just put content1_1 in your file and then use Resources.getIdentifier() like in this answer:

String title = localBufferReader.readLine();
String content_path = localBufferReader1.readLine();

// Get the resource ID
int contentResourceId = getResources().getIdentifier("raw/" + content_path, null, this.getPackage());

// Open the resource as a buffered reader
BufferedReader contentBufferReader = new BufferedReader(new InputStreamReader(getResources().openRawResource(contentResourceId)));

// Parse the contents as an int
int content = Integer.parseInt(contentBufferReader.readLine());
Community
  • 1
  • 1
Raniz
  • 10,882
  • 1
  • 32
  • 64