2011-12-11 18 views
0

FileInputStreamを使用してlistView内にimageViewを作成したい場合は、File OutputStreamを使用して自分のカメラ画像を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"); 
      } 







}; 
} 

、その後、私のコードは、私がリストビューを移入していた場所にようである、いずれかは私に私はImageViewのに画像をロードすることができる方法についてのポインタやレイアウトのコードスニペットを与えることができますか?リストビューで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); 
} 


} 

感謝ステファン

答えて

0

は、SDカード内の画像を表示するIm​​ageViewのを設定し

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

しかし、あなたがのために独自のアダプタを定義する必要が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; 
    } 
} 

ビューホルダーパターンの使い方については、this tutorialを参照してください。

これが役に立ちます。

+0

これは配列アダプタです...このメソッドはカーソルアダプタでも機能しますか? – aspiringCoder

+0

@StefanReaney SQLite dbを読んでレシピのリストを取得し、上記のようなカスタムアダプタを定義することができます。そうでなければ、少し異なったカスタムCursorAdapterを定義できます。newViewとbindViewをオーバーライドする必要があります。 ://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/) –

関連する問題