1

I am developing an application in which i need to read a resource file. I am saving my file (named "sample")in raw directory of res folder. When i tried to access it in code, by writing its resource id as R.raw.sample , it gives the following error.

Error(30,60)Cannot find symbol variable raw.

I am using intellij idea 11.0.3 and platform for android is 2.3.3.

The code is as follows...

package com.example;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException; 
import java.io.InputStream;
import java.io.InputStreamReader;

public class SampleLoader {

public SampleLoader(){

}

public static int[] readFromFile(Context ctx, int resId){
    String sample = readRawTextFile(ctx,resId);
    int [] arr =  stringToIntArray(sample);
    return arr;
}

public static String readRawTextFile(Context ctx, int resId)
{
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputReader = new InputStreamReader(inputStream);
    BufferedReader buffReader = new BufferedReader(inputReader);
    String line;
    StringBuilder text = new StringBuilder();

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

public static int[] stringToIntArray(String line){
    int[] sample = new int[line.length()-2];
    String s ;
    for (int i=0; i<line.length(); i++)
    {
        if(i>1)
        { sample[i] = Character.getNumericValue(line.charAt(i));
        }
    }
    return sample;
}

public void populateListOfSamples(){


}

}

package com.example;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyActivity extends Activity {

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(createContent());
}

LinearLayout createContent(){
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
    linearLayout.setLayoutParams(params);
    final int[] arr6 = SampleLoader.readFromFile(this,R.raw.sample) ;
    final Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.no3);


    final ImageView imageView1 = new ImageView(this);
    imageView1.setVisibility(View.INVISIBLE);

          final TextView textView = new TextView(this);
    String str2 = "Byte Array will appear here!";
    textView.setText(str2);

    final Button btn2 = new Button(this);
    final Button btn = new Button(this);
    btn.setText("Convert Pic to Byte Array");
    btn.setOnClickListener(new View.OnClickListener() { //Event handler for click event.

        public void onClick(View v) {

            textView.setText(str3);
            btn2.setVisibility(View.VISIBLE);

        }

    });



            btn2.setText("Convert Byte Array to Pic");
            btn2.setVisibility(View.INVISIBLE);
            btn2.setOnClickListener(new View.OnClickListener() { //Event handler for click event.

                public void onClick(View v) {
                    btn.setVisibility(View.INVISIBLE);
                    final Bitmap imag = filing.arrtoimg(arr6);
                    textView.setVisibility(View.INVISIBLE);
                    imageView1.setVisibility(View.VISIBLE);
                    imageView1.setImageBitmap(imag);
                }

            });



    linearLayout.addView(imageView1);
    linearLayout.addView(btn);
    linearLayout.addView(btn2);
    linearLayout.addView(textView);

    return linearLayout;
}

}

Dania
  • 1,007
  • 1
  • 14
  • 21
  • 1
    The first one which comes to my head that you have some incorrectly named files in your /res folder that's why R.java couldn't be regenerated. The second one may be conjoined with file extension, but I've never stored files in /res folder without their extension. Try out these variants. – teoREtik Sep 20 '12 at 10:56
  • Refer this post http://stackoverflow.com/questions/2856407/android-how-to-get-access-to-raw-resources-that-i-put-in-res-folder – yogeshhkumarr Sep 20 '12 at 11:08

0 Answers0