Start a new topic

please help me to upload to and download image from kinvey server

my code for taking a picture and storing in storage given below ,

but i don't know how to upload image , i tried the upload section code provided at store , but does'nt work well, could any one help me please....


here is my code........

package com.example.chotu.selfy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kinvey.android.AsyncAppData;
import com.kinvey.java.core.KinveyClientCallback;
import com.kinvey.java.core.MediaHttpUploader;
import com.kinvey.java.core.UploaderProgressListener;
import com.kinvey.java.model.FileMetaData;
import com.kinvey.java.model.KinveyMetaData;

public class AndroidSurfaceviewExample extends Activity implements SurfaceHolder.Callback {
TextView testView;
boolean recording=false;
private boolean cameraFront = false;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
private Context myContext;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
Button btn;
Button video;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


myContext = this;




setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
surfaceHolder.addCallback(this);

// deprecated setting, but required on Android versions prior to 3.0
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
Uri imageFileUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
//starting from here


//end here







try {



OutputStream imageFileOS = getContentResolver().openOutputStream(
imageFileUri);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close(); // Here I want to upload image on kinvey server // please help me ,
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Toast.makeText(getApplicationContext(), "Picture Saved", Toast.LENGTH_LONG).show();
refreshCamera();
}
};

btn=(Button)findViewById(R.id.btnCapture);
btn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(null, null, jpegCallback);
}
}

);
initialize();
}







public void captureImage(View v) throws IOException {
//take the picture
camera.takePicture(null, null, jpegCallback);
}

public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {
// preview surface does not exist
return;
}

// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
setCamera(camera);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
Log.d("", "Error starting camera preview: " + e.getMessage());
}
}




public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.

refreshCamera();
}

public void surfaceCreated(SurfaceHolder holder) {
try {
// open the camera
if (camera == null)
camera = Camera.open();
} catch (RuntimeException e) {
// check for exceptions
System.err.println(e);
return;
}
Camera.Parameters param;
param = camera.getParameters();

if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
param.set("orientation", "portrait");
camera.setDisplayOrientation(90);
}

Camera.Parameters parameters = camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90);
}
try {
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
} catch (Exception e) {
if (camera != null)
camera.release();
camera = null;
e.printStackTrace();
// mCamera.release();
}

}



public void surfaceDestroyed(SurfaceHolder holder) {
// stop preview and release camera
if(camera==null)
return;
camera.stopPreview();
camera.release();
camera = null;
}

public void setCamera(Camera camera) {
//method to set a camera instance
camera = camera;
Camera.Parameters parameters = camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90);
}

camera.setParameters(parameters);
// mCamera.setPreviewDisplay(holder);

}

private void releaseCamera() {
// stop and release camera
if (camera != null) {
camera.release();
camera = null;
}
}

public void initialize() {




video = (Button)findViewById(R.id.btnVideo);
video.setOnClickListener(switchCameraListener);
}


View.OnClickListener switchCameraListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// get the number of cameras
if (!recording) {
int camerasNumber = Camera.getNumberOfCameras();
if (camerasNumber > 1) {
// release the old camera instance
// switch camera, from the front and the back and vice versa

releaseCamera();
chooseCamera();
} else {
Toast toast = Toast.makeText(myContext, "Sorry, your phone has only one camera!", Toast.LENGTH_LONG);
toast.show();
}
}
}
};


public void chooseCamera() {
// if the camera preview is the front
if (cameraFront) {
int cameraId = findBackFacingCamera();
if (cameraId >= 0) {
video.setText(" Front ");
// open the backFacingCamera
// set a picture callback
// refresh the preview

camera = Camera.open(cameraId);
// mPicture = getPictureCallback();
refreshCamera();
}
} else {
int cameraId = findFrontFacingCamera();
if (cameraId >= 0) {
// open the backFacingCamera
// set a picture callback
// refresh the preview
video.setText(" Back ");

camera = Camera.open(cameraId);
// mPicture = getPictureCallback();
refreshCamera();
}
}
}

private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
cameraFront = true;
break;
}
}
return cameraId;
}

private int findBackFacingCamera() {
int cameraId = -1;
// Search for the back facing camera
// get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
// for every camera check
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
cameraFront = false;
break;
}
}
return cameraId;
}

public void onResume() {
super.onResume();
if (!hasCamera(myContext)) {
Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
toast.show();
finish();
}
if (camera == null) {
// if the front facing camera does not exist
if (findFrontFacingCamera() < 0) {
Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
video.setVisibility(View.GONE);
}
camera = Camera.open(findBackFacingCamera());
refreshCamera();
}
}

private boolean hasCamera(Context context) {
// check if the device has camera
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}

}

Ankush,

Please refer to this link for upload / download of files:

http://devcenter.kinvey.com/android/guides/files

  1. What is the exact error that you are getting?
  2. Also can you send a screenshot of the error?

 

Thanks,

Pranav

Kinvey Support

try{
File file = new File(""+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/aa.jpg");
/* File[] files = file.listFiles();
if(files != null){
for(File f : files){ // loop and print all file
System.out.println(f.getName()+"------------------");
// this is file name
}
}*/
// File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/aa.jpg");
//java.io.File file = new java.io.File("aa.jpg");
FileInputStream fin=new FileInputStream(file);
UploaderProgressListener upl=new UploaderProgressListener() {
@Override
public void onSuccess(Void result) {
System.out.println("upload success!");
}
@Override
public void onFailure(Throwable error) {
System.out.println("upload progress change!");
}
@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
System.out.println("upload progress change -> " + uploader.getUploadState());
}
};
mKinveyClient.file().upload("myFile1",fin,upl);
}catch(Exception e){
System.out.println("Couldn't upload! -> " + e);
e.printStackTrace();
}
System.out.println("after uploading file");
hey I am using above code to upload an image from my android app. it is uploaded on kinvey server but when I try to open it by using download link it shows "This XML file does not appear to have any style information associated with it. The document tree is shown below.

<Error>

<Code>NoSuchKey</Code>

<Message>The specified key does not exist.</Message>

</Error>

"  
but if I directly drop a file on page of kinvey server it seems fine and opened by download link. 

Screenshot%20(29).png

in this screenshot , first two images are uploaded by app and third one is dragged and dropped on this page. I don't know what is the problem here ! can you please sort it out ? 
thanks in advance


Login or Signup to post a comment