user2847219
user2847219

Reputation: 555

Android convert byte to file

With this code, I obtain the contents of a file located in the root of Google Drive. Now my requirement is to convert the array of byte [] into a file and save it in the external memory device. when debugging I get this error:

    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 method to get file from drive

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;
}

async to save file

@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;
            }

Upvotes: 0

Views: 719

Answers (1)

padonald
padonald

Reputation: 133

I would say problem is here:

File internalDB = new File(str);

The file name for internalDB is being set to the what you you are reading in from the buffer and it is throwing:

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

Upvotes: 1

Related Questions