我正在开发一个我需要读取资源文件的应用程序。我将文件(名为“sample”)保存在res文件夹的原始目录中。当我尝试在代码中访问它时,通过将其资源ID写为R.raw.sample,它会出现以下错误。
错误(30,60)无法找到符号变量raw。
我使用的是intellij idea 11.0.3,android的平台是2.3.3。
代码如下......
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;
}
}