2012-04-02 11 views
12

Eventuali duplicati:
Android Camera - Save image into a new folder in SD CardPrendere con intenti fotocamera e salvare su file

sto cercando di prendere immagine e salvarla in un file. Il problema è che sto cercando di salvare la bitmap in un file. Qui è il mio codice:

private void takePic() { 
    Intent cameraIntent = new Intent(
      android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, 2); 


} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == 2) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      ImageView test = (ImageView) findViewById(R.id.test); 
      test.setImageBitmap(photo); 

      try { 
       FileOutputStream out = new FileOutputStream("filename"); 
       photo.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

E le eccezioni in logcat:

04-02 14:46:51.975: W/IInputConnectionWrapper(2225): showStatusIcon on inactive InputConnection 
04-02 14:46:56.135: W/System.err(2225): java.io.FileNotFoundException: /filename (Read-only file system) 
04-02 14:46:56.135: W/System.err(2225):  at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
04-02 14:46:56.145: W/System.err(2225):  at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 
04-02 14:46:56.145: W/System.err(2225):  at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 
04-02 14:46:56.145: W/System.err(2225):  at java.io.FileOutputStream.<init>(FileOutputStream.java:165) 
04-02 14:46:56.145: W/System.err(2225):  at java.io.FileOutputStream.<init>(FileOutputStream.java:144) 

risposta

4

l'errore dice chiaramente che java.io.FileNotFoundException: /filename

si prega di fornire il percorso esatto "/ sdcard/filename"

new FileOutputStream(getExternalStorageDirectory()+"filename"); 

O

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/name.png"; 

nota: aggiungi permesso WRITE_EXTERNAL_STORAGE nel file manifest.

+0

mi Tels non esiste un metodo getExternalStorageDirectory() – Darko

+0

vedi, avevo aggiornato il codice. –

+0

significa che non hai una scheda SD –

7

ry il codice qui sotto è una delle soluzioni al problema ::

static Uri capturedImageUri = null; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    this.imageView = (ImageView) this.findViewById(R.id.imageView1); 
    Button photoButton = (Button) this.findViewById(R.id.button1); 
    photoButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Calendar cal = Calendar.getInstance(); 
      File file = new File(Environment.getExternalStorageDirectory(), (cal.getTimeInMillis() + ".jpg")); 
      if (!file.exists()) { 
       try { 
        file.createNewFile(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } else { 
       file.delete(); 
       try { 
        file.createNewFile(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      capturedImageUri = Uri.fromFile(file); 
      Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); 
      startActivityForResult(i, CAMERA_RESULT); 
     } 
    }); 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_REQUEST) { 
      //Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      //imageView.setImageBitmap(photo); 
      try { 
       Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), capturedImageUri); 
       imageView.setImageBitmap(bitmap); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
+7

Formattate sempre il vostro codice (CTRL + MAIUSC + F per amor di dio) quando postate allo stackoverflow, ho bisogno di due righe di questo codice e non le trovo a causa della formattazione del codice o –

+0

come posso eliminarlo prima di salvare? –

Problemi correlati