54

私はアンドロイドからサウンドボードをプログラミングしています。問題は、一部のサウンドが機能し、一部が機能しないことです。これが、機能しないサウンドに対して取得したトレースバックです。

05-31 13:23:04.227 18440 18603 W System.err: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
05-31 13:23:04.227 18440 18603 W System.err:    at android.content.res.AssetManager.openAssetFd(Native Method)
05-31 13:23:04.227 18440 18603 W System.err:    at android.content.res.AssetManager.openFd(AssetManager.java:331)
05-31 13:23:04.227 18440 18603 W System.err:    at com.phonegap.AudioPlayer.startPlaying(AudioPlayer.java:201)
05-31 13:23:04.227 18440 18603 W System.err:    at com.phonegap.AudioHandler.startPlayingAudio(AudioHandler.java:181)
05-31 13:23:04.235 18440 18603 W System.err:    at com.phonegap.AudioHandler.execute(AudioHandler.java:64)
05-31 13:23:04.235 18440 18603 W System.err:    at com.phonegap.api.PluginManager$1.run(PluginManager.java:86)
05-31 13:23:04.235 18440 18603 W System.err:    at java.lang.Thread.run(Thread.java:1096)

何か案は?

4

13 に答える 13

111

次のように、特定の拡張機能のアセット圧縮を無効にすることができます。

android {
    aaptOptions {
        noCompress "pdf"
    }
}

ソース

于 2015-10-27T03:50:08.987 に答える
49

この問題が発生したTensorflowLiteファイルを使用している人は

ブロック内のGradleファイル(android/app/build.gradle)に次の行を追加します。android{}

aaptOptions {
    noCompress "tflite"
}
于 2019-07-29T07:57:38.000 に答える
43

アセットフォルダで圧縮ファイルを開くには制限があります。これは、非圧縮ファイルをプロセスの仮想アドレス空間に直接メモリマップできるため、解凍のために同じ量のメモリが再度必要になることを回避できるためです。

Androidアプリでのアセット圧縮の処理では、圧縮ファイルを処理するためのいくつかの手法について説明します。aapt圧縮されていない拡張子(例)を使用してファイルを圧縮しないように仕向けることができます。または、作業を行う代わりに、圧縮せずmp3に手動でファイルを追加することもできます。apkaapt

于 2011-05-31T11:48:55.677 に答える
9

そのファイルの圧縮を無効にする必要があります。単に追加します:

    aaptOptions {
       noCompress "your-file-name"
    }

内部のアプリレベルのbuild.gradleファイルにandroid { }

于 2018-10-26T11:35:14.850 に答える
5

この明らかに苛立たしい状況は、.apkが構築されるときに、一部のアセットが保存される前に圧縮されるのに対し、他のアセットはすでに圧縮されたものとして扱われ(たとえば、画像、ビデオ)、そのままにしておくために発生します。後者のグループはを使用して開くことができますがopenAssetFd、前者のグループは開くことができません-しようとすると、「このファイルはファイル記述子として開くことができません。おそらく圧縮されています」というエラーが表示されます。

1つのオプションは、ビルドシステムをだましてアセットを圧縮しないようにすることです(@nicstrongの回答のリンクを参照)が、これは面倒です。より予測可能な方法で問題を回避しようとする方がよいでしょう。

私が思いついた解決策はAssetFileDescriptor、アセットのを開くことはできませんが、それでも開くことができるという事実を使用していますInputStream。これを使用して、アセットをアプリケーションのファイルキャッシュにコピーし、その記述子を返すことができます。

@Override
public AssetFileDescriptor openAssetFile(final Uri uri, final String mode) throws FileNotFoundException
{
    final String assetPath = uri.getLastPathSegment();  // or whatever

    try
    {
        final boolean canBeReadDirectlyFromAssets = ... // if your asset going to be compressed?
        if (canBeReadDirectlyFromAssets)
        {
            return getContext().getAssets().openFd(assetPath);
        }
        else
        {
            final File cacheFile = new File(getContext().getCacheDir(), assetPath);
            cacheFile.getParentFile().mkdirs();
            copyToCacheFile(assetPath, cacheFile);
            return new AssetFileDescriptor(ParcelFileDescriptor.open(cacheFile, MODE_READ_ONLY), 0, -1);
        }
    }
    catch (FileNotFoundException ex)
    {
        throw ex;
    }
    catch (IOException ex)
    {
        throw new FileNotFoundException(ex.getMessage());
    }
}

private void copyToCacheFile(final String assetPath, final File cacheFile) throws IOException
{
    final InputStream inputStream = getContext().getAssets().open(assetPath, ACCESS_BUFFER);
    try
    {
        final FileOutputStream fileOutputStream = new FileOutputStream(cacheFile, false);
        try
        {
            //using Guava IO lib to copy the streams, but could also do it manually
            ByteStreams.copy(inputStream, fileOutputStream); 
        }
        finally
        {
            fileOutputStream.close();
        }
    }
    finally
    {
        inputStream.close();
    }
}

これは、アプリがキャッシュファイルをそのままにしておくことを意味しますが、それは問題ありません。また、気にするかもしれないし気にしないかもしれない既存のキャッシュファイルを再利用しようとはしません。

于 2014-06-19T04:19:34.820 に答える
4

この例外は、次を呼び出すことでスローできます。

final AssetFileDescriptor afd = activity.getAssets().openFd(path);

res/rawアセットフォルダではなくディレクトリにファイルを保存することで問題を修正し、次のように取得しますAssetFileDescriptor

final AssetFileDescriptor afd = activity.getResources().openRawResourceFd(rawId);

その後、FileNotFoundExceptionはなくなり、ファイルは圧縮されなくなります。

于 2018-09-05T07:29:18.257 に答える
3

この例外は、を開こうとした場合にのみ発生するはずですFileDesriptor。ファイルを読むだけで、InputStreamAssetManager.open("filename.ext"))を通り抜けることができます。これは私のために働いた。

事前にファイルサイズが必要な場合は、FileDescriptorそのメソッドを呼び出すために(したがって非圧縮ファイル)が必要getLength()です。そうでない場合は、ストリーム全体を読み取ってサイズを決定する必要があります。

于 2013-07-22T13:22:35.643 に答える
2

私は歩き回った、私は使用します:

ParcelFileDescriptor mFileDescriptor = context.getAssets().openFd(file).getParcelFileDescriptor();

しかし、その結果は次のようになります。java.io.FileNotFoundException:このファイルをファイル記述子として開くことはできません。おそらく圧縮されています。

この実装の代わりに、ParcelFileDescriptorの関数を使用してファイルを直接開きます。

private void openRenderer(Context context,String fileName) throws IOException {  

File file=  FileUtils.fileFromAsset(context, fileName);
        ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_WRITE); 

        mPdfRenderer = new PdfRenderer(parcelFileDescriptor);
    }`

    public class FileUtils {
    private FileUtils() {
    }

    public static File fileFromAsset(Context context, String assetName) throws IOException {
        File outFile = new File(context.getCacheDir(), assetName );
        copy(context.getAssets().open(assetName), outFile);

        return outFile;
    }

    public static void copy(InputStream inputStream, File output) throws IOException {
        FileOutputStream outputStream = null;

        try {
            outputStream = new FileOutputStream(output);
            boolean read = false;
            byte[] bytes = new byte[1024];

            int read1;
            while((read1 = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read1);
            }
        } finally {
            try {
                if(inputStream != null) {
                    inputStream.close();
                }
            } finally {
                if(outputStream != null) {
                    outputStream.close();
                }

            }

        }

    }
}
于 2015-07-15T08:20:16.123 に答える
1

以下のようにファイルの拡張子をbuild.gradleに追加して、問題を解決しました

android {
   aaptOptions {
      noCompress "tflite"
      noCompress "txt"
      noCompress "pdf"
   }
}
于 2020-11-23T14:38:28.307 に答える
0

アセットフォルダーから取得するファイルが1MBより大きい場合は、ファイルをzipファイルとして圧縮し、使用する前に解凍して、圧縮せずに外部ストレージに保存するのが効果的です。

InputStream fileInputStream = getAssets().open("your_file.your_file_extension.zip");
unzipInputStream(fileInputStream, "your_folder_in_external_storage");

unzipInputStream私が使用した方法はこれです:

public static void unzipInputStream(InputStream inputStream, String location)
{
    try {
        if ( !location.endsWith(File.separator) ) {
            location += File.separator;
        }
        File f = new File(location);
        if(!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE));
        try {
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                String path = location + ze.getName();
                File unzipFile = new File(path);

                if (ze.isDirectory()) {
                    if(!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    createParentDirectoriesIfMissing(unzipFile);
                    unzipFile(zin, unzipFile);
                }
            }
        } finally {
            zin.close();
        }
    } catch (Exception e) {
        Log.e("", "Unzip exception", e);
    }
}

private static void createParentDirectoriesIfMissing(File unzipFile)
{
    File parentDir = unzipFile.getParentFile();
    if ( null != parentDir ) {
        if ( !parentDir.isDirectory() ) {
            parentDir.mkdirs();
        }
    }
}

private static void unzipFile(ZipInputStream zin, File unzipFile) throws IOException
{
    int size;
    byte[] buffer = new byte[BUFFER_SIZE];
    FileOutputStream out = new FileOutputStream(unzipFile, false);
    BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);

    try {
        while ( (size = zin.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
            fout.write(buffer, 0, size);
        }

        zin.closeEntry();
    } finally {
        fout.flush();
        fout.close();
    }
}
于 2018-11-10T12:32:11.337 に答える
0

gnome-sound-recorderがOGGファイルを作成するため、同じ問題が発生しました。これは、MediaPlayerを使用して再生することはできません。だから私はそれらをffmpegでMP3に変換しました、そしてそれはうまくいきました。ですから、これが最も簡単な方法だと思います。

ffmpeg -i youroggfile yournewfile.mp3

また、リソースに疑問符が付いたまま表示されていることと、R.raw.yournewfileを使用してアクセスしたときに、コードに「.mp3」拡張子を記述していないことにも気づきました。

于 2019-11-12T15:45:45.843 に答える
0

同じ問題が発生し、ファイルをres /rawから/data/data/your.app.pkg/cacheフォルダーにコピーすると、すべてうまくいきます:D

AssetFileDescriptor afd = null;
try {
    File cache = new File(getCacheDir(), "my_data.dat");
    if (!cache.exists()) {
        copyInputStreamToFile(getResources().openRawResource(R.raw.my_data), cache);
    }
    afd = new AssetFileDescriptor(ParcelFileDescriptor.open(cache, ParcelFileDescriptor.MODE_READ_ONLY), 0, AssetFileDescriptor.UNKNOWN_LENGTH);
} catch (Exception e) {
    e.printStackTrace();
}


private void copyInputStreamToFile(InputStream in, File file) {
    BufferedOutputStream bfos = null;

    try {
        bfos = new BufferedOutputStream(new FileOutputStream(file));
        byte[] buf = new byte[4096];

        int len;
        while ((len = in.read(buf)) != -1) {
            bfos.write(buf, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bfos != null) {
                bfos.close();
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2020-09-02T12:17:08.187 に答える
0

私の場合、resource.arscが原因で圧縮されています。非圧縮のresource.arscを使用して再構築すると、問題が解決します。

于 2020-12-30T04:01:38.823 に答える