我如何使用 fileinputstream 填充 listView 中的 imageview

发布于 2024-12-20 18:38:00 字数 5919 浏览 2 评论 0原文

我使用 FileoutputStream 将相机图像输出到 sdCard,然后我想使用 fileInputStream 在 listView 中填充 imageView。所以这是我的代码注释,我已经更改了我的一台计算机的路径以与模拟器一起使用。

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);
}


}

谢谢斯特凡

I'm outputting my camera image to my sdCard using FileoutputStream, then i want to populate an imageView in a listView using fileInputStream. so here is my code note i have changed the path to one of my computer for use with the emulator.

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");
            }







};
}

and then here is my code as to where i am populating the listView, can any one give me a pointer or a layout code snippet as to how i can load the image into the 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);
}


}

thanks Stefan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2024-12-27 18:38:00

设置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;
    }
}

查看本教程了解如何使用 View Holder 模式。

希望这有帮助。

Set the ImageView to show the image in SD Card

ImageView imageView;
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bmp);

But if you want to display the ImageView in the ListView, you have to define your own adapter for the ListView, and it would be better to use the View Holder pattern to improve the performance.

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;
    }
}

Check out this tutorial about how to use View Holder pattern.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文