2011-10-24 70 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);' –