Ich verwende erfolgreich this project, um eine Datei in Google Drive zu erstellen oder zu ändern. Jetzt muss ich die Datei auf den externen Speicher des Geräts herunterladen. Ich kann den Inhalt der Datei lesen, und ich kann es speichern. Aber wenn ich versuche, es auf dem Desktop zu öffnen, ist die Datei beschädigt.Android Drive api Datei herunterladen
@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 = buf == null ? "" : new String(buf);
File fl = UT.str2File(str, "myfile.db");
}
----------------------------------------------
static File str2File(String str, String name) {
if (str == null) return null;
byte[] buf = str.getBytes();
File fl = new File(Environment.getExternalStorageDirectory(), name);
if (fl == null) return null;
BufferedOutputStream bs = null;
try {
bs = new BufferedOutputStream(new FileOutputStream(fl));
bs.write(buf);
} catch (Exception e) { le(e); }
finally {
if (bs != null) try {
bs.close();
} catch (Exception e) { le(e); }
}
return fl;
}
----------------------------------------------
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;
}
Sie auf sD-Karte speichern möchten einen ByteArrayOutputStream statt Fileoutputstream erstellen haben. –