Android 如何使用fileinputstream在listView中填充imageview
我使用FileoutputStream将相机图像输出到SD卡,然后我想使用fileInputStream在listView中填充imageView。这是我的代码注释,我已经更改了一台计算机的路径,以便与仿真器一起使用Android 如何使用fileinputstream在listView中填充imageview,android,listview,android-imageview,Android,Listview,Android Imageview,我使用FileoutputStream将相机图像输出到SD卡,然后我想使用fileInputStream在listView中填充imageView。这是我的代码注释,我已经更改了一台计算机的路径,以便与仿真器一起使用 public class CameraAPI extends Activity implements SurfaceHolder.Callback{ public Camera camera; MediaRecorder mediaRecorder; CBDataBaseHelp
public class CameraAPI extends Activity implements SurfaceHolder.Callback{
public Camera camera;
MediaRecorder mediaRecorder;
CBDataBaseHelper RH;
TextView RecipeID;
public void onCreate (Bundle savedInstanceState){
boolean diditwork;
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
RecipeID = (TextView)findViewById(R.id.Rid2);
String RowID;
Bundle extras = getIntent().getExtras();
RowID = extras.getString("SELECTED2");
RecipeID.setText(RowID);
SurfaceView surface = (SurfaceView)findViewById(R.id.acccam);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}}
public void takePhoto(View view){
//String ID = RecipeID.getText().toString();
//Long LID = Long.parseLong(ID);
boolean diditwork;
try{
takePicture();
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
//String path = "/sdcard/Image.jpg";
//RH.updateRecipe3(LID,path);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (mediaRecorder == null){
try{
camera = camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
}
public void takePicture(){
boolean diditwork;
try{
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
}
ShutterCallback shutterCallback= new ShutterCallback()
{
public void onShutter(){
}
};
PictureCallback rawCallback = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
}
};
PictureCallback jpegCallback = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("//STEFAN-PC/Users/stefan/Desktop/Uni Work/Image.jpg");
outStream.write(data);
outStream.close();
}catch(FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
}catch(IOException e){
Log.d("CAMERA",e.getMessage());
}
/*RH = new CBDataBaseHelper(CameraAPI.this);
RH.open();
String ID = RecipeID.getText().toString();
Long RID = Long.parseLong(ID);
RH.updateRecipe3(RID, data);
RH.close();*/
String ID = RecipeID.getText().toString();
Long RID = Long.parseLong(ID);
RH.updateRecipe3(RID, "/sdcard/Image.jpg");
}
};
}
这是我的代码,关于我在哪里填充listView,有人能给我一个指针或布局代码片段,告诉我如何将图像加载到imageView中吗
public class CBFilter extends ListActivity {
ListView RecipeNames;
Cursor cursor;
SimpleCursorAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText, RowId;
String[] from = { CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.row};
ImageView image;
byte[] dataImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean diditwork;
try{
//java.io.FileInputStream in = openFileInput("myImage.jpg");
RecipeNames = (ListView) findViewById(android.R.id.list);
RecipeNames.setTextFilterEnabled(true);
RecipeText = (TextView) findViewById(R.id.recipeText);
adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
image = (ImageView) findViewById(R.id.RecipeImage);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}}
public void CreateNew(View view){
Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
Intent intent1 = new Intent(this, CBCreate.class);
long rowId = cursor.getLong(cursor.getColumnIndex(CBDataBaseHelper.KEY_ROWID));
String s = String.valueOf(rowId);
intent1.putExtra("SELECTED", s);
startActivity(intent1);
}
}
谢谢Stefan将ImageView设置为在SD卡中显示图像
ImageView imageView;
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bmp);
但是,如果要在ListView中显示ImageView,则必须为ListView定义自己的适配器,最好使用View Holder模式来提高性能
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
public MyArrayAdapter(Activity context, String[] names) {
super(context, R.layout.rowlayout, names);
this.context = context;
this.names = names;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null, true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.label);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.textView.setText(names[position]);
// Change the icon for Windows and iPhone
String s = names[position];
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
holder.imageView.setImageBitmap(bmp);
return rowView;
}
}
公共类MyArrayAdapter扩展了ArrayAdapter{
私人最终活动背景;
私有最终字符串[]名称;
公共MyArrayAdapter(活动上下文,字符串[]名称){
super(上下文、R.layout.rowlayout、名称);
this.context=上下文;
this.names=名称;
}
//static保存对外部类的引用并避免访问
//包含类的任何成员
静态类视窗夹{
公共影像视图;
公共文本视图文本视图;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//ViewHolder会将评估缓冲到行的各个字段
//布局
视窗座;
//如果作为参数传递,则回收现有视图
//这将节省Android上的内存和时间
//仅当所有类的基本布局相同时,此选项才有效
视图行视图=转换视图;
if(rowView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
rowView=充气机。充气(R.layout.rowlayout,null,true);
holder=新的ViewHolder();
holder.textView=(textView)rowView.findViewById(R.id.label);
holder.imageView=(imageView)rowView.findviewbyd(R.id.icon);
rowView.setTag(支架);
}否则{
holder=(ViewHolder)rowView.getTag();
}
holder.textView.setText(名称[位置]);
//更改Windows和iPhone的图标
字符串s=名称[位置];
位图bmp=BitmapFactory.decodeFile(imagePath);
holder.imageView.setImageBitmap(bmp);
返回行视图;
}
}
查看有关如何使用视图固定器图案的信息
希望这有帮助。这是一个阵列适配器。。。此方法也适用于游标适配器?@StefanReaney您可以读取您的SQLite db以获取配方列表,并像上面那样定义自定义适配器,否则您可以定义自定义游标适配器,这有点不同,您必须覆盖newView和bindView,签出