繁体   English   中英

在Android中从图库获取图像时出错

[英]Error while getting image from gallery in android

我知道这个问题被问了很多,但是是的,它对我不起作用:SI通常试图使用它,但总是出现错误消息。

这是我尝试导入的内容:

public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
{
    switch(pMenuItem.getID())
    {
        case BACK:
            createMenuChildScene();
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
            return true;
    }
}

由于某些原因,如果我在下面的代码中将onActivityResult的名称更改为on ActivityForResult,则startActivityForResult会用红色下划线显示,但此处仍用红色下划线显示。

public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.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);
        }
    }
}

在这里,获取内容解析器不起作用或未定义:/我不知道我在做什么错:/

您正在尝试图像选择吗? 您正在使用的版本已过时,仅适用于KitKat以下的版本。 我建议您使用Google提供的文档提供程序,它更快,更有效,并且不会出现OutOfMemory错误。 https://developer.android.com/guide/topics/providers/document-provider.html也适用于您的startActivityForResult应该正常工作,尝试File-> Invalidate Caches并选择Invalidate Caches并重新启动它应该解决所有不必要的错误。 祝好运!

试试这个片段。

当resultcode == Result_ok时,在onActivityResult中放置此代码

case REQ_CODE_PICK_IMAGE:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
                imageview.setImageBitmap(bitmap);

这样可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM