Android:通过意图调用图库并选择图像
使用整个站点中可用的多个代码,我构建了一个小型应用程序,它将调用图库意图,并且当选择图像时,将返回路径。
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(SDCardImagesActivity.this, "selected", 2000).show();
}
}
}
}
但是当我运行这个程序时,我得到以下异常
08-09 15:12:53.191: ERROR/AndroidRuntime(26694): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
有人可以帮助我吗?提前致谢。
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(SDCardImagesActivity.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询返回 0 行,这就是您收到错误 CursorIndexOutOfBoundsException = 尝试访问包含 0 行的游标的第一行的原因。
您确定它们的照片位于上述媒体(SD 卡)上吗?
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) ?