21

アプリケーションのプライベートストレージにローカルに保存されたファイルがあります。私はそれが存在することを確認しました、しかし私がそれを呼ぶときはいつでもBitmapFactory.decodeFileそれは常にを返しますnull

ファイルをリソースとして保存して使用するImageView.setImageResourceと、常に正常に表示されます。

何が問題ですか?

スニペットは次のとおりです。

filename = "test.png";

if (doesFileExist(filename))
    Bitmap bMap = BitmapFactory.decodeFile(filename);

私も試しました:

Bitmap bMap = BitmapFactory.decodeFile(getFilesDir().getPath()
                    + filename);
4

8 に答える 8

33

この質問は、次のように以前に回答されています: BitmapFactory.decodeFile returns null even even image exists

これはまさに私が必要としていたものでした:

String fname=new File(getFilesDir(), "test.png").getAbsolutePath();
于 2011-05-16T06:06:45.730 に答える
3

皆さん、アプリリソースに保存されているファイルは特別な方法で参照する必要があります。たとえば、ファイルがアセットにあり、「myfile.png」という名前の場合、次のように参照する必要があります。

String uriString="file:///android_asset/myfile.png";
Uri uri=Uri.parse(uriString);
于 2011-05-16T05:17:49.550 に答える
0

fileList() を試していただけますか? これ

このコンテキストのアプリケーション パッケージに関連付けられたプライベート ファイルの名前を示す文字列の配列を返します。

于 2011-05-16T04:47:10.107 に答える
0

私にとっては、「file:///storage/emulated/0/....」のようなローカルに保存されたURLから画像を取得していました(画像をキャプチャするためにPhonegapプラグインを使用しました。プラグインは必要な画像パスを提供していましたネイティブコードで使用)

これが私のために働いたコードスニペットです。

String captured_image_info = "file:///storage/emulated/0/Android/data/com.testapp/cache/1493809796526.jpg"
Uri uri=Uri.parse(captured_image_info);
largeLog("uri", "" + uri);

InputStream imageStream = getContentResolver().openInputStream(uri);

Bitmap bm = BitmapFactory.decodeStream(imageStream);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object

byte[] decodedBytes = baos.toByteArray();

Bitmap img_captured_image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
于 2017-05-03T11:16:58.550 に答える