从图库android原始图像中选择图像不是从外部打开

时间:2016-11-11 10:03:07

标签: android image android-gallery

问题是我正在尝试从图库中选择图像。之后,如果我从画廊打开原始图像,它就不会打开(黑屏即将到来)!即使我尝试从应用程序中再次选择该图片,也会出现无法加载图片等错误。

private void openGalleryForImageSelection()
{
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    try
    {
        startActivityForResult(intent, IMAGE_FROM_GALLERY);
    }
    catch(Throwable e)
    {
        Log.e(LOG_TAG,"openGalleryForImageSelection failed",e);
        Toast.makeText(this,getResources().getString(R.string.image_error),Toast.LENGTH_SHORT).show();
    }
}

请帮助我如何解决此问题。谢谢。

2 个答案:

答案 0 :(得分:0)

首先将这些权限添加到您的清单中。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

现在可以在任何地方调用showFileChooser()函数。

    private void showFileChooser() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a Image to Upload"),
                    1);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(getApplicationContext(), "Please install a Photo Viewer.",
                    Toast.LENGTH_SHORT).show();
        }

    }
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {

            }
        }
}

答案 1 :(得分:0)

openGalleryForImageSelection()方法更改为:

 public void openGalleryForImageSelection(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_FROM_GALLERY);
}

然后,您将在onActivityResult()中获取意图数据,然后通过调用getPathForImage()来获取其中的真实路径:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
         realPath = getPathForImage(ProfileActivity.this, data.getData());
        }
    }
}


 public static String getPathForImage(Context context, Uri uri)
 {
    String result = null;
    Cursor cursor = null;

    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(column_index);
            cursor.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return result;
}

使用realPath onActivityResult转换为Bitmap并将Bitmap加载到ImageView 这是做到这一点的方法。希望它能帮到你