2014-01-06 16 views
0

我需要GridView圖像的幫助,在每次捕獲新圖像後能夠更新自身。現在我的代碼允許我使用相機按鈕捕捉圖像並將其保存到SD卡中的特定文件夾。我的問題是每當我捕捉一個新的圖像,我需要重新打開我的應用程序,以便新捕獲的圖像出現,所以我需要幫助,使圖像每次捕獲後自動更新自己。有人可以幫我嗎?以下是我的代碼的一些片段。允許圖像能夠在每次捕獲圖像後自行更新

MainActivity.java

public class Uploads extends Activity implements OnClickListener { 

      ImageView btnAllShops, btnFavourites, btnUploads, btnSettings, btnBuys, 
      btnTakePhoto; 
      GridView gvUploads; 
      String name = null; 
      private String description, category, price, imagepath; 
      final static int cameraData = 0; 
      private Cursor cursor; 
      private int columnIndex; 
      public static final String PHOTO_ALBUM = "Neatpicks"; 

     @SuppressWarnings("deprecation") 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_uploads); 
     findViewById(); 
     onBackPressed(); 

    // // Set up an array of the Thumbnail Image ID column we want 
    // String[] projection = { MediaStore.Images.Thumbnails._ID }; 
    // // Create the cursor pointing to the SDCard 
    // 
    // cursor = managedQuery(
    // MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, 
    // //Which 
    // // columns 
    // // to 
    // // return 
    // null, // Return all rows 
    // null, MediaStore.Images.Thumbnails.IMAGE_ID); 
    // 
    // // cursor = getContentResolver().query(
    // MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
    // // projection, 
    // // MediaStore.Images.Media.DATA + " like ? ", 
    // // new String[] {"%/Neatpicks/%"}, 
    // // null); 
    // 
    // 
    // // Get the column index of the Thumbnails Image ID 
    // columnIndex = cursor 
    // .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

    // ---------------------------------------------------------------------------------------- 

    // request only the image ID to be returned 
    String[] projection = { MediaStore.Images.Media._ID }; 
    // Create the cursor pointing to the SDCard 
    cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      projection, MediaStore.Images.Media.DATA + " like ? ", 
      new String[] { "%Neatpicks%" }, null); 
    // Get the column index of the image ID 
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); 

    gvUploads.setAdapter(new ImageAdapter(this)); 

}// onCreate 

private void findViewById() { 
    btnAllShops = (ImageView) findViewById(R.id.btnAllShops); 
    btnFavourites = (ImageView) findViewById(R.id.btnFavourites); 
    btnUploads = (ImageView) findViewById(R.id.btnUploads); 
    btnSettings = (ImageView) findViewById(R.id.btnSettings); 
    btnBuys = (ImageView) findViewById(R.id.btnBuys); 
    btnTakePhoto = (ImageView) findViewById(R.id.btnTakePhoto); 

    gvUploads = (GridView) findViewById(R.id.gvUploads); 

    btnAllShops.setOnClickListener(this); 
    btnFavourites.setOnClickListener(this); 
    btnUploads.setOnClickListener(this); 
    btnSettings.setOnClickListener(this); 
    btnBuys.setOnClickListener(this); 
    btnTakePhoto.setOnClickListener(this); 

} 

@Override 
public void onBackPressed() { 
} 

@Override 
public void onClick(View arg0) { 
    switch (arg0.getId()) { 
    case R.id.btnAllShops: 

     break; 

    case R.id.btnFavourites: 
     Intent iF = new Intent(getApplicationContext(), Favourites.class); 
     startActivity(iF); 

     break; 

    case R.id.btnUploads: 
     Intent iU = new Intent(getApplicationContext(), Uploads.class); 
     startActivity(iU); 

     break; 

    case R.id.btnSettings: 
     Intent iS = new Intent(getApplicationContext(), 
       SettingsActivity.class); 
     startActivity(iS); 

     break; 

    case R.id.btnBuys: 
     Intent iBuy = new Intent(getApplicationContext(), Buys.class); 
     startActivity(iBuy); 

     break; 

    case R.id.btnTakePhoto: 
     Intent i = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i, cameraData); 
     break; 

    } 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap bmp = (Bitmap) extras.get("data"); 

     Intent goMain = new Intent(getApplicationContext(), 
       EditUploads.class); 

     ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, bs); 
     goMain.putExtra("byteArray", bs.toByteArray()); 
     startActivity(goMain); 
    } 
} 

public class ImageAdapter extends BaseAdapter { 
    private Context context; 

    public ImageAdapter(Context localContext) { 
     context = localContext; 
    } 

    public int getCount() { 
     return cursor.getCount(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(context); 
     // Move cursor to current position 
     cursor.moveToPosition(position); 
     // Get the current value for the requested column 
     int imageID = cursor.getInt(columnIndex); 
     // obtain the image URI 
     Uri uri = Uri.withAppendedPath(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       Integer.toString(imageID)); 
     String url = uri.toString(); 
     // Set the content of the image based on the image URI 
     int originalImageId = Integer.parseInt(url.substring(
       url.lastIndexOf("/") + 1, url.length())); 
     Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(
       getContentResolver(), originalImageId, 
       MediaStore.Images.Thumbnails.MINI_KIND, null); 
     i.setImageBitmap(b); 
     i.setLayoutParams(new GridView.LayoutParams(250, 250)); 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setPadding(8, 8, 8, 8); 
     // i.setBackgroundResource(mGalleryItemBackground); 
     return i; 

    } 
}} 

ImageAdapter.java

public class ImageAdapter extends BaseAdapter { 

private Context context; 

public ImageAdapter(Context c) { 
    context = c; 
} 

// ---returns the number of images--- 

// ---returns the ID of an item--- 
public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

// ---returns an ImageView view--- 

@Override 
public int getItemViewType(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public int getViewTypeCount() { 
    // TODO Auto-generated method stub 
    return 1; 
} 

@Override 
public boolean hasStableIds() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public boolean isEmpty() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public void registerDataSetObserver(DataSetObserver observer) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void unregisterDataSetObserver(DataSetObserver observer) { 
    // TODO Auto-generated method stub 

} 

@Override 
public boolean areAllItemsEnabled() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public boolean isEnabled(int position) { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    return null; 
}} 

任何幫助,將不勝感激!

回答

0

GridView在創建GridView時發現變量值。如果此值更改,則除非有刷新例程或onCreate()例程再次運行,否則不會更新顯示。爲了刷新圖像,我建議你看看ImageLoader的代碼。

ImageLoader創建一個單獨的線程,用於下載和更新放置在其隊列(實際堆棧)中的圖像。

的CursorAdapter代碼

public class MapDataFileCursorAdapter extends CursorAdapter { 

private LayoutInflater sInflater; 
private int fNameColumn; 
private int fSnippetColumn; 
private int fImageUrlColumn; 

public MapDataFileCursorAdapter(Context context, Cursor c, int flags) { 
    super(context, c, flags); 

    fNameColumn = c.getColumnIndex(MapDataFileDBAdapter.KEY_NAME); 
    fSnippetColumn = c.getColumnIndex(MapDataFileDBAdapter.KEY_SNIPPET); 
    fImageUrlColumn = c.getColumnIndex(MapDataFileDBAdapter.KEY_IMAGEURL); 

    sInflater = LayoutInflater.from(context); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView name = (TextView) view.findViewById(R.id.poiDocInfoNameRow); 
    name.setText(cursor.getString(fNameColumn)); 

    TextView info = (TextView) view.findViewById(R.id.poiDocInfoSnippetRow); 
    info.setText(cursor.getString(fSnippetColumn)); 

    ImageView image = (ImageView) view.findViewById(R.id.poiDocInfoLogo); 
    String imageUrl = cursor.getString(fImageUrlColumn); 

    if (URLUtil.isValidUrl(imageUrl)) { 
     image.setEnabled(true); 
     image.setVisibility(View.VISIBLE); 
     MapDataFileListView.getImageLoader().DisplayImage(imageUrl, null, image);   
    } else { 
     image.setImageResource(R.drawable.ic_globe);    
    } 

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return sInflater.inflate(R.layout.mapdatafile_row, null); 
} 

以及指定在ListActivity

private void setUpListViewIfNeeded() { 
    if (poiListView == null) { 
     poiListView = (ListView) findViewById(android.R.id.list); 
    } 
    if (poiListView != null) { 

     Cursor data = poiDbhelper.getCursorOfAllEntries(true, null); 
     if (data != null) { 
      dataSource = new MapDataFileInstalledCursorAdapter(poiListViewContext, data, 0); 
      poiListView.setAdapter(dataSource);    
     } 
     showToastMessage(getString(R.string.toast_mesg_records_read) + " " + data.getCount()); 
    } 
} 

數據源的GridView和ListView差別很小的代碼。從這裏開始應該很容易。此外,還有兩個與ImageLoader相關的類(或至少是我使用的版本)。給我一個電子郵件地址,我會發給你們三個人(我不記得在互聯網上我把它們拿走了)。

+0

我必須爲這個圖像加載器創建一個新類嗎? – KnockFall

+0

ImageLoader是一個新類。您需要在CursorAdapter中添加對DisplayImage的調用(請參閱上面的添加代碼)。還有其他與ImageLoader相關的類,但類可以自行運行。 –

+0

是否可以教我如何將代碼集成到我的代碼中,而不是真的很確定ImageLoader如何與我的代碼一起工作。 – KnockFall