Ho visto il tuo snippet di codice e mi sembra di aver fatto lo stesso.
Ti posto il codice magari trovi tu l'errore dove io non lo vedo
FotoActivity:
codice:
public class FotoActivity extends Activity implements SurfaceHolder.Callback{
ProgressDialog pd;
Intent iFoto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mod_foto);
// Prelevo l'id utente da MappaActivity
iFoto = getIntent();
String pkg = getPackageName();
utente = iFoto.getStringExtra(pkg+".utente");
via = iFoto.getStringExtra(pkg+".via");
citta = iFoto.getStringExtra(pkg+".citta");
// SCATTA FOTO
Button buttonPicture = (Button) findViewById(R.id.camera_surface_button);
buttonPicture.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
mSurfaceView = (SurfaceView)findViewById(R.id.camera_surface);
mSurfaceView.setClickable(true);
mSurfaceHolder = mSurfaceView.getHolder(); //recupero l'holder della surfaceview
mSurfaceHolder.addCallback(this); //faccio la bind alla nostra activity
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //tipo di surface, suggerito nei tutorial ufficiali
pd = null;
}
@Override
public void onPause(){
super.onPause();
if(pd != null)
pd.dismiss();
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
nomeFoto = String.format("%d", System.currentTimeMillis());
outStream = new FileOutputStream(String.format("/sdcard/" + nomeFoto + ".jpg"));
outStream.write(data);
outStream.close();
// creo e avvio asynctask per scattare la foto
FotoGetTask task = new FotoGetTask();
task.execute("");
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
if (mPreviewRunning)
mCamera.stopPreview();
//setto le preferenze
Camera.Parameters p = mCamera.getParameters(); //prendo le preferenze della camera
p.setPreviewSize(arg2, arg3);
ArrayList<Size> list = (ArrayList<Size>) p.getSupportedPictureSizes(); //recupero le risoluzioni supportate dalla camera
int picture_width = list.get(list.size()-1).width;
int picture_height = list.get(list.size()-1).height;
p.setPictureSize(picture_width, picture_height); //setto la camera alla risoluzione più bassa
p.setJpegQuality(50); // qualità compressione JPEG
p.setFlashMode("on");
p.setFocusMode("auto");
// salvo le pref
mCamera.setParameters(p);
try {
//lancio la preview
mCamera.setPreviewDisplay(arg0);
mCamera.startPreview();
mPreviewRunning = true;
} catch (IOException e) {
//gestione errore
}
}
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera == null) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
iFoto.putExtra("pUtente",utente);
iFoto.putExtra("pVia",via);
iFoto.putExtra("pCitta",citta);
setResult(RESULT_CANCELED,iFoto);
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
finish();
}
private class FotoGetTask extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(FotoActivity.this,"Foto","Caricamento in corso...",true,false);
}
protected String doInBackground(String... params) {
//Upload del file verso il server ftp
client = new FTPClient();
try {
InetAddress indirizzo = InetAddress.getByName("www.miodominio.it");
client.connect(indirizzo);
//aperta la connessione al server procedo al login
boolean loggato = client.login("mio@dominio.it", "miapwd");
//apro il file contenuto nel path /sdcard come stream
client.setFileType(FTP.BINARY_FILE_TYPE); // imposto il tipo di dati, da modificare a seconda dell'utilità
File sdcardDir = Environment.getExternalStorageDirectory(); //prendo il riferimento alla memoria esterna
File file = new File(sdcardDir, nomeFoto + ".jpg"); //prendo il riferimento al file salvato sulla sd
FileInputStream fis = new FileInputStream(file); //creo uno stream in input a partire dal mio file
boolean upload_ok = client.storeFile("/miodominio.it/img/"+ nomeFoto + ".jpg", fis);
fis.close(); //procedo alla chiusura dello stream, infatti storeFile non chiude fis
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return nomeFoto;
}
@Override
protected void onPostExecute(String foto) {
pd.dismiss();
if (foto.equals("")){
Toast.makeText(FotoActivity.this, "Connessione dati assente", Toast.LENGTH_LONG).show();
finish();
}else{
iFoto.putExtra("pFoto",foto+".jpg");
iFoto.putExtra("pUtente",utente);
iFoto.putExtra("pVia",via);
iFoto.putExtra("pCitta",citta);
setResult(RESULT_OK,iFoto);
finish();
Toast.makeText(FotoActivity.this, "Foto caricata con successo!", Toast.LENGTH_LONG).show();
}
}
}
}
MappaActivity:
codice:
protected void onActivityResult(int requestCode, int resultCode, Intent pIntent) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
foto = pIntent.getStringExtra("pFoto");
}
if (resultCode == RESULT_CANCELED){
foto = "immagine_non_disponibile.jpg";
}
utente = pIntent.getStringExtra("pUtente");
via = pIntent.getStringExtra("pVia");
citta = pIntent.getStringExtra("pCitta");
showDialog(CUSTOM_DIALOG_ID);
}
}
grazie 1000