I have a weird bug I'm hoping someone can help me with.
I am using the Google Drive app folder to backup some data from my app. It creates the file and writes my data without and issue, I can sign grab the file from drive at any time and it works fine.
However if I uninstall the app when I reinstall it the Google Drive App folder is empty. I'm assuming I'm missing some point of the sync process so it's only caching locally but I can't see what I've done wrong. My code to create and commit the file is below.
DriveFile file = ..//Get drive file;
file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
#Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Problem opening file, status is " + result.getStatus().toString());
return;
}
Log.i(TAG, "File opened");
writeFileContents(result.getDriveContents());
}
});
private void writeFileContents(final DriveContents contents) {
new AsyncTask<Object, Object, Integer>() {
#Override
protected Integer doInBackground(Object[] params) {
try {
ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
// Overwrite the file.
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
.getFileDescriptor());
Writer writer = new OutputStreamWriter(fileOutputStream);
SyncUtils.writeXml("some xml", writer);
writer.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
#Override
protected void onPostExecute(final Integer x) {
contents.commit(mGoogleApiClient, null).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
#Override
public void onResult(com.google.android.gms.common.api.Status result) {
if(result.getStatus().isSuccess())
Log.i(TAG, "Write succeeded");
else
Log.i(TAG, "Write failed");
onConnectionCallbacks.onSynced(x);
}
});
}
}.execute();
According to the documentation, contents.getParcelFileDescriptor() only works with DriveFile.MODE_READ_WRITE. You are using DriveFile.MODE_WRITE_ONLY, so you should be calling contents.getOutputStream().
Related
I have a problem with image upload from my android mobile phone to a FTP Area, the FTP work fine, i tried some other interaction with it and i don't have any problem.
The uploaded file weight 0k, name and file extension are right, no error or exception. I do a test to copy the file in another folder and the file copied right.
private class FTPUploader extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
String data = "/pathimage/filename.jpg";
File sourceFile = new File(data);
FTPClient con = new FTPClient();
try {
con.connect("ftp.ftpServer.it", 21);
if (con.login("user", "password")) {
con.setFileType(FTP.BINARY_FILE_TYPE);
con.enterLocalPassiveMode();
if (sourceFile.exists()) {
InputStream input = new FileInputStream(sourceFile);
boolean result = con.storeFile("/folder/newfilename.jpg", input);
input.close();
if (result) {
System.out.println(result);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (sourceFile.exists()) {
con.logout();
con.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
What i'm wrong about? it's possible manage the event when the upload is finished? thank you in advance for the support
In the end, I find a workaround that works for me. I use the library ftp4j where I can manage the events of the upload and with the System.out.println I can follow the step and be sure for the right upload.
I post in to follow the code. At first, I need to add the library in the libs folder and import it in the build.gradle(Module:app)
compile files('libs/ftp4j-1.7.2.jar')
next i import what i need in the worksheet:
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
And change the code for ftp4j
private class FTPUploader extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
FTPClient con = new FTPClient();
try {
con.setPassive(true);
con.setType(FTPClient.TYPE_BINARY);
con.connect("ftp.server.it");
con.login("user", "password");
con.changeDirectory("changeFTPUploadDirectory");
con.upload(new java.io.File("/path/local/image"), new MyTransferListener());
} catch (Exception e) {
e.printStackTrace();
}
try {
con.disconnect(true);
} catch (IOException | FTPException | FTPIllegalReplyException e) {
e.printStackTrace();
}
return null;
}
}
But what I find very useful is the event management where if there is some problem you can check where and why upload is stopped.
public class MyTransferListener implements FTPDataTransferListener {
public void started() {
System.out.println("Trasferimento FTP avviato");
}
public void transferred(int length) {
System.out.println("in trasferimento: "+ String.valueOf(length));
}
public void completed() {
System.out.println("Completato");
}
public void aborted() {
System.out.println("Annullato");
}
public void failed() {
System.out.println("Trasferimento fallito");
}
}
As already stated in the top of the post this isn't a solution but a workaround to understand the error.After the edit the code with this change work right. in any way, I search the server-side logs to have more details about my problem. Thank you in advance
documentation -> ftp4j
I'm using CloudBoost for Android application and i had some problems when I try to save save a data in a table, I'm not getting any results. This is my code:
CloudApp.init("*****", "*****");
...
new Thread(new Runnable() {
#Override
public void run() {
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1"); //Already this is not shown
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
public void done(final CloudObject x, final CloudException e) {
if(e != null)
//error
Log.e("LOG", "Errore");
if(x!=null)
//cloudObject
Log.e("LOG", "FATTO");
}
});
} catch (CloudException e) {
e.printStackTrace();
}
}
});
Your log isn't shown because you made a Thread without calling start().
Android typically uses Asynctask anyway, but I'm not sure why you really need a Thread with this library... That save method looks asynchronous
CloudApp.init("*****", "*****")
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1");
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
I've succeeded onto transferring files onto my Dropbox account.
But when i share my app for testing with other members of the project, they can't transfer the files on their own Dropbox account.
And when they test the functionality, it transfers automatically onto my account.
Is there something I'm missing out here?
I know that i don't need to add manually users, they can automatically transfer the files they want.
Here's a snip of the method i used :
private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) {
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final String uploadPathF = uploadPathFrom;
final String uploadPathT = uploadPathTo;
Thread th = new Thread(new Runnable() {
public void run() {
File tmpFile = null;
try {
tmpFile = new File(uploadPathF);
} catch (Exception e) {
e.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(tmpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}
I uploaded the videos in facebook using facebook sdk 3.6. I was successfully uploaded the videos but I want to share the location as well while uploading the file. How to do that?
I am using the following code for uploading the video file. I tried but I am not getting. Please can anyone help me on this.
try {
Request videoRequest = Request
.newUploadVideoRequest(session,
file,
new Request.Callback() {
#Override
public void onCompleted(
Response response) {
if (response
.getError() == null) {
Toast.makeText(
AndroidCamera.this,
"video shared successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
AndroidCamera.this,
response.getError().getErrorMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
Bundle parameters = videoRequest.getParameters();
parameters.putString("caption",
"Video");
if (Nim_Constants.bUseLocation) {
parameters.putString("name",
Nim_Constants.Location);
}
parameters
.putString("link",
"https://developers.facebook.com/android");
videoRequest.setParameters(parameters);
// Execute the request in a separate thread
videoRequest.executeAsync();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Finally I found solution. It's pretty simple. Use the Request class instead of using newUploadVideoRequest() method because it doesn't allow to pass parameters like title, description. So use the Request class if you want to set the title, desrcription. For this, Just Pass the file as ParcelFileDescriptor and pass location(What ever you have to pass) as description.
Upload Video on Facebook
File file = new File(filepath);
try {
ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
Bundle parameters = new Bundle();
parameters.putParcelable(file.getName(),descriptor);
parameters.putString("description","Location");
Request videoRequest = new Request(session,"me/videos", parameters,HttpMethod.POST,
new Request.Callback() {
#Override
public void onCompleted(
Response response) {
if (response.getError() == null) {
Toast.makeText(
AndroidCamera.this,
"video shared successfully",
Toast.LENGTH_SHORT)
.show();
} else {
System.out
.println(response
.getError()
.getErrorMessage());
Toast.makeText(
AndroidCamera.this,
response.getError()
.getErrorMessage(),
Toast.LENGTH_SHORT)
.show();
}
}
});
// Execute the request in a separate thread
videoRequest.executeAsync();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I have an object in charge of opening a file on HDFS to write. This object renames the file it just wrote once the close() method is invoked.
The mechanism works when running in local mode, but it fails to rename the file in cluster mode.
//Constructor
public WriteStream() {
path = String.format("in_progress/file");
try {
OutputStream outputStream = fileSystem.create(new Path(hdfs_path+path), new Progressable() {public void progress() { System.out.print("."); }
});
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Did you experience that before ?
Apparently FileSystem.rename(Path) creates missing directories on the path when executed in local mode, but it does not when run in cluster mode.
This code works in both modes:
public void close() {
String dirPath = String.format("%s_dir/", date, timestamp);
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.mkdir(new Path(hdfs_path+dirPath));
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Just curious, but how can you rename a file that officially doesn't exist (because you're still writing at that point)?
The fix is to rename after the file has been completed. That is, when you invoked the close method.
So your code should look like this:
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
writer.close();
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
} catch (IOException e) {
e.printStackTrace();
}
}