簡體   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