我有一个列表活动中的文件名列表。当用户单击文件名时,应加载该图像。我通过意图传递文件名,该位工作正常。接收活动可以获得文件名。这个活动有一个自定义视图,这就是问题所在。自定义视图应该能够从 sdcard 流式传输在其活动中收到的正确文件名,并在视图中显示生成的位图。我可以在活动中获取文件名,但视图中的文件名为空。我怎样才能解决这个问题?
在此先感谢马特。
package com.tecmark;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class ShowDistortion extends Activity{
private static final String TAG = "*********showdistortion";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdistortion);
String fn = getIntent().getStringExtra("filename");
Log.e(TAG, "filename = " + fn);
final ShowDistortedBitmap sdb = (ShowDistortedBitmap)findViewById(R.id.showdistortedbitmap);
sdb.filename = fn;
}
}
.
package com.tecmark;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class ShowDistortedBitmap extends View{
private static final String TAG = "*********ShowDistortedBitmap";
private File tempFile;
private byte[] imageArray;
private Bitmap finalBitmap;
protected String filename;
public ShowDistortedBitmap(Context context, AttributeSet attrs) {
super(context, attrs);
tempFile = new File(Environment.getExternalStorageDirectory().
getAbsolutePath() + "/"+filename);
Log.e(TAG, "filename ====== "+filename);
imageArray = new byte[(int)tempFile.length()];
try{
InputStream is = new FileInputStream(tempFile);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
int i = 0;
while (dis.available() > 0 ) {
imageArray[i] = dis.readByte();
i++;
}
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
finalBitmap = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length);
if(finalBitmap == null){
Log.e(TAG, "finalbitmap = null");
}else{
Log.e(TAG, "finalbitmap = not null");
}
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.e(TAG, "******about to draw finalBitmap ");
canvas.drawBitmap(finalBitmap, 0, 0, null);
}
}