Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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内置查看器中查看图像_Android_Android Activity_Android Imageview - Fatal编程技术网

在android内置查看器中查看图像

在android内置查看器中查看图像,android,android-activity,android-imageview,Android,Android Activity,Android Imageview,我需要在android默认图像查看器中查看图像,当我执行以下代码时,它会显示相册列表,而不是特定的图像。请纠正我 MediaScannerConnection.scanFile(this, new String[] {receiptPath}, new String[] {"image/*"}, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted

我需要在android默认图像查看器中查看图像,当我执行以下代码时,它会显示相册列表,而不是特定的图像。请纠正我

MediaScannerConnection.scanFile(this, new String[] {receiptPath}, new String[] {"image/*"}, new MediaScannerConnection.OnScanCompletedListener()
{

    @Override
    public void onScanCompleted(String path, Uri uri)
    {
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.setType("image/*");
        startActivity(intent);
    }
});

我已经找到了我问题的答案,请看下面的答案

@Override
        public void onScanCompleted(String path, Uri uri)
        {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "image/*");
            startActivity(intent);
        }
在分析了android Intent源代码之后,我学到的是,如果在Intent构造函数中设置uri,然后设置type,那么数据uri会自动设置为null,因此我无法在image viewer中访问特定的图像


recepitPath是文件夹还是文件?它是文件ex./mnt/sdcard/com.example.folder/receipt.pngif它是文件,这可能会有所帮助。抱歉,仍然无法查看图像。通常不需要设置类型。类型是从数据中推断出来的。我只在没有数据的情况下使用
setType
(即,该类型仅用于解析目的或指定结果类型)。@keyboard但在某些情况下,如果我尝试查看pdf文档,如果我没有提供类型,我会得到ActivityNotFound Exception。如果我指定类型,我可以使用Office suite app查看文档,有时Android将无法推断mimetype,这就是setDataAndType()方法的作用。不过,对于图像,它通常不会有问题。