繁体   English   中英

如何从Android上的外部存储打开.gif文件作为InputStream

[英]How to open .gif file as InputStream from external storage on android

我正在尝试从外部存储(图片目录)加载.gif图像,但是我正在使用以下代码获取“找不到文件异常”

    InputStream mInputStream = null;
    AssetManager assetManager = getResources().getAssets();
    try {
        mInputStream = assetManager.open(getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath().concat("/01.gif"));          

    } catch (IOException e) {           
        e.printStackTrace();
    }

我也使用手动路径进行了测试,但出现了相同的异常

mInputStream = assetManager.open("file:///mnt/sdcard/Android/data/com.shurjo.downloader/files/Pictures/01.gif");

清单文件中有SD卡的写入/读取权限

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

请帮助我如何从外部存储中将文件作为InputStream打开。 提前致谢。

注意:我已经在模拟器上对其进行了测试,“图片”文件夹下有一个文件01.gif(请参见手册路径)。 我可以创建目录并将文件放在这些目录中,但是无法通过Input Stream访问这些文件。

AssetManager用于访问应用程序包的Assets文件夹中的文件。 它不能用于访问外部存储器中的文件。

您可以使用以下内容:

final String TAG = "MyAppTag";

File picturesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = null;
final int readLimit = 16 * 1024;
if(picturesDir != null){
    imageFile = new File(picturesDir, "01.gif");
} else {
    Log.w(TAG, "DIRECTORY_PICTURES is not available!");
}
if(imageFile != null){
    mInputStream =  new BufferedInputStream(new FileInputStream(imageFile), readLimit);
    mInputStream.mark(readLimit);
} else {
    Log.w(TAG, "GIF image is not available!");
}

还请查看getExternalFilesDir提供的示例代码

从更新:

暂无
暂无

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

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