Android: Invoke Gallery via intent and select an Image
using several codes available throughout the site, I built a small application that would invoke Gallery intent and when a image is selected the path is retured.
public class SDCardImagesActivity extends Activity {
final int REQ_CODE_PICK_IMAGE= 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header);
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQ_CODE_PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
Toast.makeText(SDCardImagesActivi开发者_Go百科ty.this, "selected", 2000).show();
}
}
}
}
But when I run this program I get following exception
08-09 15:12:53.191: ERROR/AndroidRuntime(26694): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Can someone help me out. Thanks in advance.
Your query is returning 0 rows, that's why you are getting an error CursorIndexOutOfBoundsException = trying to access first row of a cursor with 0 rows.
Have you made sure their are pics on the said media (SD card) ?
精彩评论