我需要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;
}}
任何幫助,將不勝感激!
我必須爲這個圖像加載器創建一個新類嗎? – KnockFall
ImageLoader是一個新類。您需要在CursorAdapter中添加對DisplayImage的調用(請參閱上面的添加代碼)。還有其他與ImageLoader相關的類,但類可以自行運行。 –
是否可以教我如何將代碼集成到我的代碼中,而不是真的很確定ImageLoader如何與我的代碼一起工作。 – KnockFall