• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Auto upload of pictures to ftp

Upvote 0
Hi,
I am working on that project,
It works ...one shot, then it crash.
I have created 2 activities, I m using alarmManager.setRepeating to repeat the camera thread.
If you have time to help, here is my code::thinking:
Manifest with 2 activity
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="exo.com.FtpCam"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FtpCam"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>:confused::confused:
        <activity android:name=".AndroidCamera" android:label="@string/app_name">
       </activity>
        <service android:name=".MyAlarmService" />
    </application>
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.FTP" />
    <uses-permission android:name="android.permission.WIFI" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permision.WRITE_EXTERNAL_STORAGE" />
</manifest>

The main
Code:
package exo.com.FtpCam;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FtpCam extends Activity {

private PendingIntent pendingIntent;

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

     super.onCreate(savedInstanceState);

     Intent myIntent = new Intent(FtpCam.this, AndroidCamera.class);
     :confused::confused:pendingIntent = PendingIntent.getActivity(FtpCam.this, 0, myIntent, 0);

     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(System.currentTimeMillis());
     calendar.add(Calendar.SECOND, 5);
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20*1000, pendingIntent);}}

The camera
Code:
package exo.com.FtpCam;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); //on utilise le xml main
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
      getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (SurfaceView)findViewById(R.id.camerapreview); //la zone dans le xml 
      surfaceHolder = surfaceView.getHolder();
      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }

// ici les differentes callback :
  
  AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){

      @Override
      public void onAutoFocus(boolean arg0, Camera arg1) {
       //take photo larque la photo est OK
       camera.takePicture(null, null, myPictureCallback_JPG);
       //camera.takePicture(null, null, null);
      }};
      
  PictureCallback myPictureCallback_JPG = new PictureCallback(){
  
    public void onPictureTaken(byte[] arg0, Camera arg1) {
      // TODO Auto-generated method stub
      Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
      OutputStream imageFileOS;
      try {
       imageFileOS = getContentResolver().openOutputStream(uriTarget);
       imageFileOS.write(arg0);
       imageFileOS.flush();
       imageFileOS.close();

       /*Toast.makeText(AndroidCamera.this,
         "Image saved: " + uriTarget.toString(),
         Toast.LENGTH_LONG).show();*/
       
      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
    }};

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
// TODO Auto-generated method stub
if(previewing){
 camera.stopPreview();
 previewing = false;
}

if (camera != null){
 try {
  camera.setPreviewDisplay(surfaceHolder);
  camera.startPreview();
  previewing = true;

  //Autofocus, la photo est prise dans la callback, qui indique que l image est nette 
  camera.autoFocus(myAutoFocusCallback);


 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}


// Mise en pause de l'application
@Override
public void onPause() {
    super.onPause();

    if (camera != null) {
        camera.release();
        camera = null;
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
//super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
}
 
Upvote 0
iwebcam.jpg
 
Upvote 0
Features
- upload last picture taken with camera to FTP
- take pictures every X secs
- set name of picture on the ftp side with option to replace the current picture or add a number to the file name

I plan on using it to upload the last picture taken with my camera to a blog or website. I had an app on my WM phone before switching.

Foldersync works pretty well. The photos taken on our phones are auto named by date and time, so I haven't needed that feature. If the option isn't there, there may be a separate app for such auto renaming.

Also, you may only need the lite version of Foldersync. Hope that helps! :D
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones