Android将字节转换为文件

时间:2016-05-25 16:11:17

标签: android file drive

使用此代码,我获取位于Google云端硬盘根目录中的文件内容。现在我的要求是将byte []数组转换为文件并将其保存在外部存储设备中。调试时我得到这个错误:

    at libcore.io.IoBridge.open(IoBridge.java:456)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at com.spjanson.gdaademo.MainActivity$4.doInBackground(MainActivity.java:276)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at com.spjanson.gdaademo.MainActivity$4.doInBackground(MainActivity.java:250)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at java.lang.Thread.run(Thread.java:818)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err: Caused by: android.system.ErrnoException: open failed: ENAMETOOLONG (File name too long)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at libcore.io.Posix.open(Native Method)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  ... 9 more

从驱动器获取文件的静态方法

static byte[] read(String id) {
    byte[] buf = null;
    if (mGAC != null && mGAC.isConnected() && id != null) try {
        DriveFile df = Drive.DriveApi.getFile(mGAC, DriveId.decodeFromString(id));
        DriveContentsResult rslt = df.open(mGAC, DriveFile.MODE_READ_ONLY, null).await();
        if ((rslt != null) && rslt.getStatus().isSuccess()) {
            DriveContents cont = rslt.getDriveContents();
            buf = UT.is2Bytes(cont.getInputStream());
            cont.discard(mGAC);    // or cont.commit();  they are equiv if READONLY
        }
    } catch (Exception e) {
        UT.le(e);
    }
    return buf;
}

保存文件的异步

@Override
            protected Void doInBackground(Void... params) {
                mBusy = true;
                ArrayList<ContentValues> cvs = GDAA.searchDB(UT.FILE_NAME);
                if (cvs != null) for (ContentValues cv : cvs) {
                    String gdid = cv.getAsString(UT.GDID);
                    System.out.println("ID..... " + gdid);
                    byte[] buf = GDAA.read(gdid);

                    String str = null;
                    if (buf != null) {
                        try {
                            str = new String(buf, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        FileChannel source = null;
                        FileChannel destination = null;
                        File sd = new File(Environment.getExternalStorageDirectory(), "myfile.db");
                        String backupDBPath = "myfile.db";
                        File internalDB = new File(str);
                        File backupDB = new File(sd, backupDBPath);
                        try {
                            source = new FileInputStream(internalDB).getChannel();
                            destination = new FileOutputStream(backupDB).getChannel();
                            destination.transferFrom(source, 0, source.size());
                            source.close();
                            destination.close();

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

                        }

                        //String str = buf == null ? "" : new String(buf);
                        //File fl = UT.str2File(str, "database.db");
                        }


                }

                return null;
            }

1 个答案:

答案 0 :(得分:1)

我会说问题在这里:

File internalDB = new File(str);

internalDB的文件名被设置为您从缓冲区读取的内容并且它正在抛出:

android.system.ErrnoException: open failed: ENAMETOOLONG (File name too long)