0

i'm quite the beginner in Android, i want to develop an activity where the user sees two random Images of his phone gallery, and he has to choose which one's the older one.

So i have this code i found in a tutorial and used it. It works, that it shows every image in the SD Card.

But now is my question:

how do i only get 2 random pics in my gridview?

I hope you can help me, i don't quite get it with this cursor stuff.

public class MainActivity extends Activity {

/**
 * Cursor used to access the results from querying for images on the SD
 * card.
 */
private Cursor cursor;
/*
 * Column index for the Thumbnails Image IDs.
 */
private int columnIndex;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 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);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

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


}

/**
 * Adapter for our image files.
 */
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_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView
                    .setLayoutParams(new GridView.LayoutParams(300, 300));
        } else {
            picturesView = (ImageView) convertView;
        }
        return picturesView;
    }
}

}

Tommy
  • 27
  • 1
  • 1
  • 7
  • If you have any trouble with this just ask me – SmulianJulian Jun 29 '13 at 15:15
  • Have you see this? http://stackoverflow.com/questions/13571651/find-a-random-picture-from-gallery – SmulianJulian Jun 29 '13 at 15:20
  • Yep i've seen this, but i don't know how i only use 2 pictures instead of all available ones. maybe if i shorten the string array String[] projection = { MediaStore.Images.Thumbnails._ID }; to the index of 2? – Tommy Jun 29 '13 at 17:16
  • After doing the query on the database, you should get a cursor which is a reference to the results, which in this case are IDs of the images on the SD card. Next, call cursor.getCount() to get the total number of images available then pass that into Random.nextInt() to get a randomly selected picture. Since you want 2 random pictures, you'll need to call Random.nextInt() twice. – Android Noob Jun 29 '13 at 18:56
  • hmm.. my problem now is how can i tell the imageadapter to get only the 2 images? i dont know how it works. does the cursor move on, after every getView() call? – Tommy Jun 29 '13 at 21:12

1 Answers1

0

Use StartActivity for result This will not get you two images. It lets the user see their image folders and allows them to choose. I'm new at Android too but this should help you. This works just fine for me

In MAIN get rid of all that and use

private String selectedImagePath;
private static final int SELECT_PICTURE = 1;    

im1= (ImageView)findViewById(R.id.im1);

final Bundle bundle=this.getIntent().getExtras();
final int pic=bundle.getInt("image");
im1.setImageResource(pic);

somebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}});

CREATE THIS NEW CLASS

public class GetImageActivity1 extends Activity {

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;
private ImageView img;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);

    img = (ImageView)findViewById(R.id.showImg);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                }
            });

    Button send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


            Intent intent=new Intent();
            setResult(RESULT_OK, intent);
            Bundle bundle=new Bundle();
            bundle.putInt("image",R.id.showImg);
            intent.putExtras(bundle);
            finish();




        }
    });

    Button cancel = (Button) findViewById(R.id.cancel);
    cancel.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            finish();

        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);


}

public static String getImgPathFromGallary(
        MenuView1Activity menuView1Activity, Uri imguri) {
    // TODO Auto-generated method stub
    return null;
}
}
SmulianJulian
  • 801
  • 11
  • 33