简体   繁体   中英

Doesn't read file

first I want to apologize for my bad english, I am from Slovakia ;)

So there is my problem

My friend wants to create a very simple translate aplication, very very very simple. We are beginners in programming for android and at all in java. The problem is, that our apk works great in Eclipse so we decided to create apk file and install it in our device. So when we installed it and there was a problem, our apk in device doesnt read file where are our words. So we tried it again in eclipse emulator and doesnt work too, but before creating apk it was fully working. Our file is in res/raw/dictionary

Here is our code

package com.example.dictionary;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView Vstup;
TextView Vystup;
Button presun;
String slovo;
String word;
String found;
Boolean click;
int i;
int j;
String sub;
String strf;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Vstup = (TextView)findViewById(R.id.editText1);
    Vystup = (TextView)findViewById(R.id.textView2);

    presun = (Button)findViewById(R.id.button1);
    presun.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v)
        {


                try
                {
                    slovo = Vstup.getText().toString(); 
                    InputStream is = getResources().openRawResource(R.raw.dictionary);

                    InputStreamReader inputStreamReader = new InputStreamReader(is);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    while((strf = bufferedReader.readLine()) != null)
                    {
                        i = strf.indexOf(":"); // vrati prvu poziciu retazca 
                        j = strf.indexOf(",");
                        sub = strf.substring(0,i); //vyberie zo stringu podretazec od indexu 0 po i
                        if(slovo.equals(sub))
                        {
                            found = strf.substring(i+1,j);
                            word = ("Výstup: " + found);
                            Vystup.setText(word.toString());

                        }
                        else {
                            word = ("Výstup: Word not found");
                            Vystup.setText(word.toString());
                        }

                    }
                    bufferedReader.close();

                }

                catch(Exception e)
                {
                    System.out.println(e);
                }
            }               



    });


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



}

error logcat

error opening trace file: no such file or directory(2)

getResources().openRawResource() gets an InputStream for a file in the res/raw/ directory of your Android project. The argument to openRawResource() is an int named R.raw. filename where filename is the name of the file without the extension.

So, the file won't be in the res/assets/ directory.

Although you can doublecheck that the file you're trying to read is actually in res/raw, it seems odd to me that you could build the APK and get an R.raw. filename entry if the file wasn't actually there. I'd use a debugger to step through the code and check the variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM