每次捕获图像后,允许图像自行更新

时间:2014-01-06 11:47:16

标签: java android image

我需要帮助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;
}}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分: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关联的类(或至少我正在使用的版本)。给我一个电子邮件地址,我会发给你所有这三个(我不记得在互联网上我把它们送到了哪里)。