在java中将InputStream转换为MappedByteBuffer?

时间:2013-10-27 08:22:06

标签: java android

我正在尝试创建一个Android应用。

我使用InputStream inputStream=getAssets().open("book.txt");来阅读book.txt;

我只想从InputStream中获取MappedByteBuffer对象;

谁知道?

========= ===========

我使用FileInputStream fis = (FileInputStream) inputStream;导致错误! 这是错误日志:

10-27 10:45:55.830: D/dalvikvm(9243): GC_EXTERNAL_ALLOC freed 22K, 53% free 2577K/5379K, external 1916K/2428K, paused 62ms
10-27 10:45:55.940: D/dalvikvm(9243): GC_EXTERNAL_ALLOC freed 2K, 53% free 2577K/5379K, external 2516K/2516K, paused 69ms
10-27 10:45:56.230: I/fileName(9243): sahala.txt
10-27 10:45:56.240: D/szipinf(9243): Initializing inflate state
10-27 10:45:56.240: D/AndroidRuntime(9243): Shutting down VM
10-27 10:45:56.240: W/dalvikvm(9243): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-27 10:45:56.260: E/AndroidRuntime(9243): FATAL EXCEPTION: main
10-27 10:45:56.260: E/AndroidRuntime(9243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silenceper.bookdemo/com.silenceper.book.shl.activity.ReadBookActivity}: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.os.Looper.loop(Looper.java:123)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at java.lang.reflect.Method.invoke(Method.java:507)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at dalvik.system.NativeStart.main(Native Method)
10-27 10:45:56.260: E/AndroidRuntime(9243): Caused by: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.silenceper.book.shl.utils.BookPageFactory.openBookFromInputStream(BookPageFactory.java:83)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.silenceper.book.shl.activity.ReadBookActivity.initBookData(ReadBookActivity.java:71)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.silenceper.book.shl.activity.ReadBookActivity.onCreate(ReadBookActivity.java:58)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-27 10:45:56.260: E/AndroidRuntime(9243):     ... 11 more

3 个答案:

答案 0 :(得分:0)

<强>更新

要执行此操作,您需要先将资产复制到文件,然后将以下技术应用于文件。如果您的资产超过1MB,则需要将其拆分 - Load files bigger than 1M from assets folder


您无法使用InputStream执行此操作。您应该为FileInputStream获取book.txt或者投射inputStream,然后从中获取FileChannel

try {
    FileInputStream fis = (FileInputStream) inputStream;
    FileChannel channel = fis.getChannel();
    ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    byte[] bytes = new byte[102];
    buffer.get(bytes);
    System.out.println(new String(bytes));
} catch (IOException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

我也有类似的需要。这是我做的:
   1)使用Asset Manager打开资源文件,然后将常规文件写入/data/data/com.yourpackage/files目录。示例代码:

private void prepareModelFile() throws IOException {
        AssetManager assetManager = getAssets();
        String configDir = getFilesDir().getAbsolutePath();
        InputStream stream = assetManager.open("mytestfile_in_assets.data");
        mTFLiteModelFile = configDir +"/mytestfilename.data";
            OutputStream output = new BufferedOutputStream(new FileOutputStream(mTFLiteModelFile));
            copyFile(stream, output);
    }

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();;
    out.close();
}

2)现在,您已从资产中复制了文件,可以使用以下方式创建MappedByteBuffer。示例:

        FileInputStream inputStream = new FileInputStream(mTFLiteModelFile);
        FileChannel fileChannel = inputStream.getChannel();
        MappedByteBuffer myMappedBuffer =  fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());

希望这会有所帮助。

大卫

答案 2 :(得分:-1)

你不能这样做,出于很多原因,大多数原因都暗示这个问题没有意义。