我正在制作android应用程序..其中我已经将我的图像字符串编码到外部卡中的外部文件..但是当我读取该文件以显示图像....应用程序崩溃.. 这是一个代码:
storedpic.setOnClickListener(new View.OnClickListener() {
ImageView storedimageView = (ImageView) findViewById(R.id.imageView3);
public void onClick(View v) {
File sdcard = Environment.getExternalStorageDirectory();
File readfile = new File(sdcard, "myfile.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(readfile));
String test;
while (true) {
test = br.readLine();
// readLine() returns null if no more lines in the file
byte[] bytarray = Base64.decode(test, Base64.DEFAULT);
Bitmap bmimage = BitmapFactory
.decodeByteArray(bytarray, 0, bytarray.length);
storedimageView.setImageBitmap(bmimage);
if (test == null)
break;
}
}
catch (IOException e) {
Toast.makeText(getBaseContext(), "Can't read a file", Toast.LENGTH_LONG).show();
}
}
});
答案 0 :(得分:0)
由于输入NPE
为String
,Base64#decode
投掷null
的可能性很大。尝试替换
while (true) {
test = br.readLine();
...
}
与
while ((test = br.readLine()) != null) {
...
}
当然会删除break
声明