Android用户必须从他的图库中的两张图片中进行选择

Android用户必须从他的图库中的两张图片中进行选择,android,gallery,Android,Gallery,我是Android的初学者,我想开发一个活动,让用户看到他的手机图库的两个随机图像,他必须选择哪个是旧的 所以我在教程中找到了这段代码,并使用了它。它的工作原理是,它显示SD卡中的每个图像 但现在我的问题是: 如何在gridview中仅获取2张随机图片? 我希望你能帮我,我不太明白光标的意思 public class MainActivity extends Activity { /** * Cursor used to access the results from querying fo

我是Android的初学者,我想开发一个活动,让用户看到他的手机图库的两个随机图像,他必须选择哪个是旧的

所以我在教程中找到了这段代码,并使用了它。它的工作原理是,它显示SD卡中的每个图像

但现在我的问题是:

如何在gridview中仅获取2张随机图片?

我希望你能帮我,我不太明白光标的意思

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;
    }
}

}

使用StartActivity获得结果 这不会给你两张图片。它允许用户查看他们的图像文件夹,并允许他们进行选择。我也是Android新手,但这应该对你有帮助。 这对我来说很好

总的来说,把这些都扔掉,然后使用它

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);
}});
创建这个新类

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;
}
}

如果你对此有任何问题,就问我你看到这个了吗?是的,我看过这个,但我不知道我怎么只用两张图片而不是所有可用的。也许我可以缩短字符串数组string[]projection={MediaStore.Images.Thumbnails.\u ID};在数据库上执行查询后,您应该得到一个游标,它是对结果的引用,在本例中是SD卡上图像的ID。接下来,调用cursor.getCount()以获取可用图像的总数,然后将其传递到Random.nextInt()以获取随机选择的图片。由于需要两张随机图片,因此需要调用random.nextInt()两次。。我现在的问题是如何告诉imageadapter仅获取这两个图像?我不知道它是怎么工作的。每次调用getView()后,光标是否继续移动?