0

in my app I take picture and save it, now I want display it in gridView and repeat this process to build some kind of gallery photos taken by me. It should looks like (First screen) : enter image description here

So at the start this gallery should be emptny then I will fill it.

here is what I got already

public class GuidePhotoAlbum extends Activity {

    private static final int TAKE_PICTURE = 1;
    Button button1;
    private Uri imageUri;
    GridView gridview;
    Bitmap help1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide_photo_album);
        gridview = (GridView) findViewById(R.id.gridView);
        button1 = (Button)findViewById(R.id.buttonCam);
        //imageView = (ImageView)findViewById(R.id.imageView) ;

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TakePictureIntent();
            }
        });
    }

    private void TakePictureIntent()
    {
        int count = 0;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" +"v" +count+".jpg"));
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(takePictureIntent, TAKE_PICTURE);
        count++;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == TAKE_PICTURE)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                try {
                    help1 = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);
                    //imageView.setImageBitmap(thumbnail.extractThumbnail(help1,help1.getWidth(),help1.getHeight()));
                    gridview.setAdapter(new ImageAdapter(this));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
            super.onActivityResult(requestCode, resultCode, data);
    }

    public class ImageAdapter extends BaseAdapter
    {

        public ImageAdapter(GuidePhotoAlbum guidePhotoAlbum) {

        }

        @Override
        public int getCount() {
            return 0;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    }

Don't exactly know what should I do inside this ImageAdapter to add this picture and display it. One more thing If will be a lot of images it should scroll.

Here XML to

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/guide_photo_album"
    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="com.example.kylu.layout.GuidePhotoAlbum">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0sp"
        android:layout_weight="3">


       <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/gridView"
            android:numColumns="3"
            android:horizontalSpacing="5dp"
            android:verticalSpacing="5dp"
            android:stretchMode="columnWidth">

        </GridView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0sp"
        android:layout_weight="1">
        <Button
            android:text="Take Picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:onClick="onClick"
            android:id="@+id/buttonCam" />
    </LinearLayout>
    </LinearLayout>
</RelativeLayout>

P.S Sorry for weak english

1 Answers1

0

Get All images shot by Camera by following method

public void List<String> getCameraImages(Context context) {
final String[] projection = { MediaStore.Images.Media.DATA };
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
        projection, 
        selection, 
        selectionArgs, 
        null);
ArrayList<String> result = new ArrayList<String>(cursor.getCount());
if (cursor.moveToFirst()) {
    final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    do {
        final String data = cursor.getString(dataColumn);
        result.add(data);
    } while (cursor.moveToNext());
}
cursor.close();
return result;

}

Change your imageAdpater construtor like this

public ImageAdapter(Context context,List<String>list) {
     this.imageList = list;
    }

and put following code in getview method of your imageAdapter

ImageView imageView;
     if (convertView == null) {  // if it's not recycled, initialize some attributes
         imageView = new ImageView(mContext);
         imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

     } else {
         imageView = (ImageView) convertView;
     }

     Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);

     imageView.setImageBitmap(bm);
     return imageView;
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22