2011-10-24 67 views
0

我正在使用以下代碼拍攝照片,使用設備相機。我是android新手。任何人都可以幫助我,告訴我應該在哪裏指定路徑。我想將圖像保存在SD卡的單獨文件夾中。任何幫助深表謝意。在SD卡中保存相機圖片

private static final int CAMERA_PIC_REQUEST = 2500; 

bcontinue.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
    } 
}); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (requestCode == CAMERA_PIC_REQUEST && resultCode==RESULT_OK) 
     { 

      try{ 
       Byte image1 = (Byte) data.getExtras().get("data"); 
        FileOutputStream fos = openFileOutput("filename.bmp", Context.MODE_PRIVATE); 
       fos.write(image1); 
       fos.close(); 
        } 
        catch(Exception e){ 

        }   
       Bitmap image = (Bitmap) data.getExtras().get("data"); 
       ImageView imageview = (ImageView) findViewById(R.id.imageView1);    
       imageview.setImageBitmap(image); 

       Context context = getApplicationContext(); 
       CharSequence text = "Click on the image!"; 
       int duration = Toast.LENGTH_LONG; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 
     } 

回答

0

下面的代碼將啓動默認的攝像頭,並有攝像頭的圖像保存到指定的URI。關鍵是將額外的「MediaStore.EXTRA_OUTPUT」與所需的uri一起放入。

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + image_name + ".jpg"); 
Uri imageUri = Uri.fromFile(file); 

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

startActivityForResult(intent, 0); 
+0

謝謝哥們,將檢查回來與評論很快 –

+0

兄弟我有更多的doubt.I要保存圖片一個在我的SD卡被稱爲「全部」文件夾中。我現在應該如何設定這條路? –

+0

sry它花了我很長時間纔回來..呃,如果我沒有記錯的話,如果你只是改變你打開的路徑/ All/...你應該沒問題..例如: 'File file = new File(Environment.getExternalStorageDirectory()。getPath()+「/ All /」+ image_name + .jpg);' –