开发者

Updating gridview from onactivityresult

I have a grid view and when user click on image, I will send intent to start a camera and take picture. From on activity result I will get a image path and I will get the image from that path, now how to update that image in the place of grid item.

here is my code:

public class ImageGrid extends Activity {
    /** Called when the activity is first created. */


    ImageAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        mAdapter = new ImageAdapter(this);
        gridview.setAdapter(mAdapter);

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Toast.makeText(ImageGrid.this, "" + position, Toast.LENGTH_SHORT).show();
                ((ImageView) v).setImageResource(R.drawable.icon);

                startCameraActivity(position);
            }
        });

    }


    protected void startCameraActivity(int position)
    {
        File file = new File("/sdcard/"+ position +".png");
        Uri outputFileUri = Uri.fromFile( file );

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

        startActivityForResult( intent, position );
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        Log.i( "MakeMachine", "resultCode: " + resultCode );
        switch( resultCode )
        {
            case 0:
                Log.i( "MakeMachine", "User cancelled" );
                break;

            case -1:
                onPhotoTaken(requestCode);
                break;
        }
    }


    protected void onPhotoTaken(int position)
    {

        Bitmap image = getPreview("/sdcard/"+ position +".png");
    // how to update the grid view image.

    }


    /*
     * Create a bitmap image by reading the image data from the path provided.
     */
    public Bitmap getPreview(String path) {
        File image = new File(path);

        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(image.getPath(), bounds);
        if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
            return null;

        int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
                : bounds.outWidth;

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = originalSize / 100;
        return BitmapFactory.decodeFile(image.getPath(), opts);
    }


    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

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

        public int getCount() {
            return mThumbIds.length;
        }

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

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

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  /开发者_如何学C/ if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
        };
    }
}


The quick thing I'd suggest is to hang on to the selected ImageView:

public class ImageGrid extends Activity {

    ImageAdapter mAdapter;
    ImageView mSelectedView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        /** ... **/ 
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                mSelectedView = (ImageView) v;
                mSelectedView.setImageResource(R.drawable.icon);
                startCameraActivity(position);
            }
        });

    }

and then update it in onPhotoTaken:

protected void onPhotoTaken(int position)
{
    Bitmap image = getPreview("/sdcard/"+ position +".png");
    mSelectedView.setImageBitmap(image);
}

Don't forget that you will also need to update your ImageAdapter class so that it will use the new image when the GridView invokes getView() for that position.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜