更新GridView拍照后

时间:2014-03-15 22:18:15

标签: android gridview android-gridview

我是这个Android编程的新手,但是我一直在寻找一个anwser for this。

该应用程序拍摄照片,然后在gridView中将其呈现给用户,但问题是,在您关闭应用程序之前它没有显示,然后转到手机的内置库中查看图片,当你再次进入应用程序时,它再次显示一切正常。

下面是代码:

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:text="@string/cheese" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

主要活动:

    package com.example.camerainteration;

import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;



public class MainActivity extends Activity implements OnClickListener {

    Button camera;
    ImageView imagem;
    private static int TAKE_PICTURE = 1;
    private Uri outputFileUri;
    private Cursor cursor;
    private int columnIndex;
    GridView sdcardImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        camera = (Button)findViewById(R.id.camera);
        camera.setOnClickListener(this);
        refresh();



    }

private void refresh(){
    String[] projection = {MediaStore.Images.Thumbnails._ID};

    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);

    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    sdcardImages = (GridView) findViewById(R.id.gridView1);
    sdcardImages.setAdapter(new ImageAdapter(this));
}

private void TakePhoto() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(), "mnt/sdcard/DCIM/100ANDRO/");


    outputFileUri = Uri.fromFile(file);
    Uri uriSavedImage = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(intent, TAKE_PICTURE);




    }

    private 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 picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
                picturesView.setPadding(10, 10, 10, 10);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    @Override
    public void onClick(View v) {

        switch(v.getId()){
        case R.id.camera:
            TakePhoto();
            refresh();
            break;

        }
    }
}

任何帮助将不胜感激,并提前感谢您的时间

2 个答案:

答案 0 :(得分:0)

String[] projection = {MediaStore.Images.Thumbnails._ID}; 

cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which columns to return null, // Return all rows null, MediaStore.Images.Thumbnails.IMAGE_ID);


columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

sdcardImages = (GridView) findViewById(R.id.gridView1); sdcardImages.setAdapter(new ImageAdapter(this));

将上面的代码放在方法中并调用它两次。一旦进入onCreate然后再拍摄照片

答案 1 :(得分:0)

从TakePhoto返回后,您不应立即调用刷新。您需要实现onActivityResult方法,然后从那里进行刷新。你已经通过 startActivityForResult 做了正确的事。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK){
        if (requestCode == TAKE_PICTURE) {
            refresh()
        }
    }

还有一件事。您可能根本不需要调用刷新。您可以在适配器上执行 notifyDataSetChanged ,这也应该可以。

祝你好运。