【问题标题】:Android: Invoke Gallery via intent and select an ImageAndroid:通过意图调用图库并选择图像
【发布时间】:2011-08-09 09:55:37
【问题描述】:

使用整个站点中可用的几个代码,我构建了一个小型应用程序,该应用程序将调用 Gallery 意图,并在选择图像时返回路径。

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

谁能帮帮我。提前致谢。

【问题讨论】:

    标签: android android-sdcard android-gallery


    【解决方案1】:

    您的查询返回 0 行,这就是您收到错误 CursorIndexOutOfBoundsException = 试图访问具有 0 行的游标的第一行的原因。

    您确定它们是上述媒体(SD 卡)上的图片吗?

    【讨论】:

    • 是的,有图片。当我选择一个图像时,只抛出这个异常。数组出界:(
    • 这是一个权限问题。我试图访问我使用我的应用程序创建的图像,似乎没有特权。当我尝试使用原生相机拍摄的图像时,它就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多