Android:通过意图调用图库并选择图像

发布于 2024-11-28 18:32:54 字数 1712 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2024-12-05 18:32:54

您的查询返回 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) ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文