在SD卡中保存相机图片

时间:2011-10-24 04:45:59

标签: android

我使用以下代码使用设备相机拍照。我是android的新手。任何人都可以帮助我,告诉我应该在哪里指定路径。我想将图像保存在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();
        }

1 个答案:

答案 0 :(得分: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);