Trying to upload an image from android app to cloud using kinvey - java

I'm trying to upload an image, saved in the internal memory of my phone, clicked by an android app to a cloud service, and am using Kinvey to do so. But I'm facing some problems with it.
Every time I run the code that contains the upload part, I encounter an exception. I'm uploading an image of ".png" type. Any conversion to blob is not a necessary process that is required, unlike that in Google Cloud Platform.
Here is my .java code -
`Client mKinveyClient = new Client.Builder(APP_KEY, SECRET_KEY, this.getApplicationContext()).build();
mKinveyClient.enableDebugLogging();
mKinveyClient.ping(new KinveyPingCallback() {
public void onFailure(Throwable t) {
Log.e(TAG, "Kinvey Ping Failed", t);
}
public void onSuccess(Boolean b) {
Log.d(TAG, "Kinvey Ping Success");
}
});
java.io.File file = new java.io.File(Environment.getExternalStorageDirectory().getPath() + "/Camera/" + "IMG_20161115_193353.jpg");
mKinveyClient.file().upload(file, new UploaderProgressListener() {
public void onSuccess(Void result) {
Log.i(TAG, "successfully upload file");
}
#Override
public void onSuccess(FileMetaData fileMetaData) {
Log.i(TAG, "successfully uploaded file");
}
#Override
public void onFailure(Throwable error) {
Log.e(TAG, "failed to upload file.", error);
}
#Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
Log.i(TAG, "upload progress: " + uploader.getUploadState()); // all updates to UI widgets need to be done on the UI thread
}
});`
Now, although the ping call is giving me a successful response, the upload part is giving me an error.
E/Activity file: failed to upload file.
com.kinvey.java.KinveyException:
REASON: No user is currently logged in.
I've searched a lot on this topic, on kinvey's discussion platform and here too. But I'm still stuck. I don't know where I'm going wrong or what I might be missing.
If anyone is out there who has successfully managed to upload images through kinvey then please help me out.

Any kind of Kinvey operations must be provided via a logged user.
You must sign up (or login) before you start to i/o any information.
mKinveyClient.user().create("username", "password", new KinveyUserCallback()) {
#Override
public void onFailure(Throwable t) {
CharSequence text = "Could not sign up.";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onSuccess(User u) {
//here we go on uploading file
}
});
You can find more info about users here

Sharmistha,
Yes, every app action needs an active user context.
So you will need to login in the app and then upload the file.
Please take a look at the following:
http://devcenter.kinvey.com/android/guides/users#ActiveUser
http://devcenter.kinvey.com/android/guides/files#Uploading
Thanks,
Pranav
Kinvey

Related

how to (integrate)call post API with 2 parameters and 1 PDF file in android java

How to attach PDF file to post API with two parameter? I am using Fast android networking library.
I am able to call API but I when user touched button my API called in my API have three parameters like this:
message = "Test"
receiver_Email = "#gmail.com"
File = text.PDF;
Sy API allows only PDF form met with message and email. I am using Fast android networking library. I try to call API but I am not able to do it.
I also looked at some examples but it couldn't help me out.
just call this method from your onCreate this is the easy way to call API with file and parameter I hope it helps you
//method used to call API to send email
enter code here
#RequiresApi(API = Build.VERSION_CODES.LOLLIPOP_MR1)
public void call_Api()
{
final String key = "file";
final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/test.pdf");
AndroidNetworking.initialize(this);
AndroidNetworking.upload("your API")
.setPriority(Priority.HIGH)
.addMultipartParameter("message", "test")
.addMultipartParameter("receiverEmail","testrg0017#gmail.com")
.addMultipartFile(key, file)
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(new JSONObjectRequestListener()
{
#Override
public void onResponse(JSONObject response)
{
Log.d("res ",response.toString());
if(file.exists())
{
Toast.makeText(PdfGeneration.this, "API call successfully",
Toast.LENGTH_SHORT).show();
}
}
#Override
public void onError(ANError anError)
{
anError.printStackTrace();
Log.d("res12 ",anError.toString());
if(!file.exists())
{
Toast.makeText(PdfGeneration.this, "file not available",
Toast.LENGTH_SHORT).show();
}
}
});
}

Cleaning local data from realtime-database remotely or after consecutive crashes

My realtime-database has data persistence for offline use and recently a recording error recorded a String as HashMap and consequently the application crashed for all my clients. I quickly corrected the error, but now all clients can no longer open the application, as it loads offline before online and ends up slowing down the error and closing the application for all users, or a good part of them.
I'm instructing them to do data wiping manually, but this is a big problem for me and I need to find a way to force this data wiping of all users remotely or predict in the application that when there are some crashes it will clear the local data automatically.
Any code suggestions or how can I do this?
I solve this problem with an try/catch, like code below:
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
try {
User user = dataSnapshot.getValue(User.class); // error occurs here
//... some code ...//
} catch (DatabaseException databaseException) {
Log.e(TAG, "DatabaseException ****** The read failed: " + databaseException);
clearPreferences(context.getApplicationContext());
deleteCache(context.getApplicationContext());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { Log.e(TAG, "onCancelled ****** The read failed: " + databaseError); }
public static void clearPreferences(Context context) {
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+context.getPackageName());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception ignored) {}
}

Uploading a file from android phone that contains a space in its path

I'm having a hard time passing files with spaces in their names into Twilio chat.
Forgetting about the fact that I'm using Twilio, and just concentrating that I'm trying to upload a file from within the Android phone. I have an image on my android phone which I found its path as /Internal storage/Download/4893079.jpg
Below is the full code:
public void testing() {
Messages messagesObject = channel.getMessages();
try {
messagesObject.sendMessage(
Message.options()
.withMedia(new FileInputStream("/Internal%20storage/Download/4893079.jpg"), "image/jpeg")
.withMediaFileName("4893079.jpg")
.withMediaProgressListener(new ProgressListener() {
#Override
public void onStarted() {
System.out.println("Upload started");
}
#Override
public void onProgress(long bytes) {
System.out.println("Uploaded " + bytes + " bytes");
}
#Override
public void onCompleted(String mediaSid) {
System.out.println("Upload completed");
}
}),
new CallbackListener<Message>() {
#Override
public void onSuccess(Message msg) {
System.out.println("Successfully sent MEDIA message");
}
#Override
public void onError(ErrorInfo error) {
System.out.println("Error sending MEDIA message");
}
});
} catch (FileNotFoundException e) {
System.out.println("FAILED HORRIBLY");
System.out.println(e);
e.printStackTrace();
}
}
However running the app I end up with the error,
java.io.FileNotFoundException: /Internal%20storage/Download/4893079.jpg (No such file or directory)
As shown I've tried replacing space with %20, but that didn't work. I've also tried \ , \\ . However non of them worked. I've looked at Opening a local Android File with spaces, Save file in Android with spaces in file name.
How should I be replacing the space?
You need to get the actual path programmatically. So the real path is
Environment.getExternalStorageDirectory().toString() + "/Download/4893079.jpg"
Internal storage is just something the user sees, but not a real path on the machine.

Google Drive Api can't open all file formats

Hi I tried to open some files from Google Drive in andoid app using Google Drive Api with:
ResourceClient.openFile(selectedFile.asDriveFile() , DriveFile.MODE_READ_WRITE)
My code worked just fine for Word format (docx), but for all ohters format i tried it threw
com.google.android.gms.common.api.ApiException: 10: This file is not openable.
exception. This exception is nowhere to be found and i really couldn't get rid of it. If anybody can help I would be really grateful.
EDIT: Btw. I claimed metadata for all files successfully.
EDIT 2: Even while using Google samples i could open just Word documents.
Code that I used just in case you would need it:
Signing in to google:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build();
signInClient = GoogleSignIn.getClient(getApplicationContext(), gso);
startActivityForResult(signInClient.getSignInIntent(), SING_IN_REQEST_CODE);
Geting metadata and files:
contentsTask = resourceClient.getMetadata(selectedFile.asDriveResource())
.continueWithTask(new Continuation<Metadata, Task<DriveContents>>(){
#Override
public Task<DriveContents> then(#NonNull Task<Metadata> task) throws Exception {
if(task.isComplete() && task.isSuccessful()) {
Log.d(TAG, "Metadata claimed sucessfully");
if(task.getResult().isEditable())
Log.d(TAG, "File is edittable");
return resourceClient.openFile(selectedFile.asDriveFile() , DriveFile.MODE_READ_WRITE);
}
else {
Log.i(TAG, "Metadata wasn't claimed sucessfully" + task.isComplete());
return null;
}
}
} ).addOnSuccessListener(new OnSuccessListener<DriveContents>() {
#Override
public void onSuccess(DriveContents driveContents) {
Log.i(TAG, "successfully get driveContents");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.i(TAG, "didn't successfully get driveContents", e);
}
});
EDIT: Issue has been replicated by: https://github.com/googledrive/android-demos/issues/70
So, I left and come back this topic 2 times, but for others: Despide the fact that i didn't find ANY DOCUMENTATION that would say that, Android Drive API (and my friend told me exactly same expirience with Java API) can only download files:
PDF
Pictures
and can not download:
office formats
If anybody would have anything to add (like I overlooked huge documentaion on topic wich formats can you acces with Drive APIs please let me know.-

Opening file geodatabase using ESRI android API

for some time I am trying to open file godatabase on android using ESRI api in version 10.2.
I have a file godatabase made with Arccatalog 10.1. It contains one layer. I can open it in Arcmap so everything looks fine here.
The geodatabase is in folder named android.gdb
I copied it to microSD card and tried to open it using this code:
new com.esri.core.gdb.Geodatabase("/mnt/sdcard2/android.gdb");
The "/mnt/sdcard2/android.gdb" file exists and is a folder and I have read and write permissions.
I get a RuntimeException with the message that the goedatabase file could not be opened.
Anyone had similiar issues with that ?
The ESRI runtime api for android wont be able to open that kind of database. It uses a proprietary version of a geodatabase built in SQLLite. You will need to publish a service in ArcGisOnline or ArcServer 10.2 and call the GeodatabaseSyncTask object in order to get a version of your database in the right format onto the device. On your published feature service you will need to make sure Sync is enabled. Then you can utilize this code to call your feature service and store it locally. This code is based on this ESRI sample -- https://developers.arcgis.com/android/sample-code/offline-editor/
public void LoadGdb(UserCredentials credentials, Polygon extent, SpatialReference spatRef){
mapExtent = extent;
mapSpatialRef = spatRef;
String replicaUrl = callingActivity.getResources().getString(R.string.feature_service_url);
gdbTask = new GeodatabaseSyncTask(replicaUrl, credentials);
gdbTask.fetchFeatureServiceInfo(new CallbackListener<FeatureServiceInfo>() {
#Override
public void onError(Throwable e) {
Log.e(TAG, "", e);
}
#Override
public void onCallback(FeatureServiceInfo objs) {
if (objs.isSyncEnabled()) {
requestGdbInOneMethod(gdbTask, mapExtent, mapSpatialRef);
}
}
});
}
protected void requestGdbInOneMethod(GeodatabaseSyncTask geodatabaseSyncTask, Polygon extent, SpatialReference spatRef) {
GenerateGeodatabaseParameters params = new GenerateGeodatabaseParameters({0, 1}, extent,
spatRef, true, SyncModel.LAYER, spatRef);
CallbackListener<String> gdbResponseCallback = new CallbackListener<String>() {
#Override
public void onCallback(String obj) {
try {
// This onCallback gets called after the generateGeodatabase
// function on the GeodatabaseSyncTask is called.
// You can store a reference to this database or you can load it
// with your code and point it to the gdbFileName location
Geodatabase myGeodatabase = (Geodatabase)obj;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
#Override
public void onError(Throwable e) {
Log.e(TAG, "", e);
}
};
GeodatabaseStatusCallback statusCallback = new GeodatabaseStatusCallback() {
#Override
public void statusUpdated(GeodatabaseStatusInfo status) {
showMessage(callingActivity, status.getStatus().toString());
}
};
// !! THE gdbFileName is a string of the path and filename
// where the geodatabse will be stored.
geodatabaseSyncTask.generateGeodatabase(params, gdbFileName, false, statusCallback, gdbResponseCallback);
}
To load a local file you need to use ArcGIS Desktop 10.2.1 or higher to generate a Runtime Geodatabase file with a *.geodatabase file extension. See the instructions here: http://resources.arcgis.com/en/help/main/10.2/index.html#//00660000045q000000

Categories

Resources