Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 允许图像在每次捕获图像后进行自我更新_Java_Android_Image - Fatal编程技术网

Java 允许图像在每次捕获图像后进行自我更新

Java 允许图像在每次捕获图像后进行自我更新,java,android,image,Java,Android,Image,我需要帮助gridview图像能够在每次捕获新图像后自动更新。现在,我的代码允许我使用相机按钮拍摄图像,并将其保存到SD卡中的特定文件夹中。我的问题是,每当我捕获一个新图像时,我都需要重新打开我的应用程序,以便显示新捕获的图像,因此我需要帮助使图像在每次捕获后自动更新。有人能帮我吗?下面是我的一些代码片段 MainActivity.java public class Uploads extends Activity implements OnClickListener {

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

任何帮助都将不胜感激

GridView在创建GridView时查找变量值。如果此值更改,则除非有刷新例程或再次运行onCreate()例程,否则不会更新显示。为了刷新图像,我建议您查看的代码

ImageLoader创建一个单独的线程,用于下载和更新放置在其队列(实际上是堆栈)中的图像

游标适配器代码

 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相关的类(或者至少是我正在使用的版本)。给我一个电子邮件地址,我会把这三个都发给你(我记不起在互联网上从哪里下载的)。

我必须为此imageloader创建一个新类吗?imageloader是一个新类。您需要在游标适配器中添加对DisplayImage的调用(请参阅上面添加的代码)。还有其他与ImageLoader相关的类,但该类可以自行运行。您可以教我如何将您的代码集成到我的代码中吗,不太清楚ImageLoader是如何与我的代码一起工作的。它不是我的代码,但它是一个很棒的软件,我希望我已经编写了它:)我通常将它与带有游标适配器的ListActivity一起使用。我会在我的第一个回复中添加更多的代码。非常感谢。您可能希望将它们发送到此电子邮件。魔术师_9@hotmail.com