Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:通过intent调用Gallery并选择图像_Android_Android Sdcard_Android Gallery - Fatal编程技术网

Android:通过intent调用Gallery并选择图像

Android:通过intent调用Gallery并选择图像,android,android-sdcard,android-gallery,Android,Android Sdcard,Android Gallery,使用整个站点中的几个代码,我构建了一个小应用程序,该应用程序将调用Gallery intent,并且当选择图像时,将返回路径 public class SDCardImagesActivity extends Activity { final int REQ_CODE_PICK_IMAGE= 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(s

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

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

有人能帮帮我吗。提前感谢。

您的查询返回了0行,这就是为什么您得到一个错误CursorIndexOutOfBoundsException=试图访问具有0行的光标的第一行


您是否确保他们的照片在上述媒体(SD卡)上?

是的,有图像。当我选择图像时,只会引发此异常。ArrayoutofBond:(这是一个权限问题。我试图访问我用应用程序创建的图像,但似乎没有权限。当我尝试使用本机相机拍摄的图像时,它就像一个魔咒一样工作。