2013-06-29 35 views
0

我是Android的初學者,我想開發一個活動,用戶可以看到他的手機圖庫中的兩個隨機圖像,並且他必須選擇哪一個更老一。Android用戶必須從他的圖庫中選擇兩張圖片

所以我有這個代碼,我在教程中找到並使用它。它的工作原理是它顯示SD卡中的每個圖像。

但現在是我的問題:

我怎麼只得到2張隨機圖片在我的GridView的?

我希望你能幫助我,我不太明白這個光標的東西。

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

}

+0

如果您有任何這方面的麻煩就問我 – SmulianJulian

+0

你有沒有看到這個? http://stackoverflow.com/questions/13571651/find-a-random-picture-from-gallery – SmulianJulian

+0

是的,我見過這個,但我不知道如何使用2張圖片,而不是所有可用的。也許如果我縮短字符串數組String [] projection = {MediaStore.Images.Thumbnails._ID};到2的指數? – Tommy

回答

0

使用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; 
} 
} 
相關問題