在相机活动中将图像从drawable保存到SD卡

时间:2012-04-17 20:27:47

标签: android image camera save drawable

我正在开发一个项目,通过ImageView从drawable文件夹中读取图像文件,通过

成功加载
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setOnTouchListener(this);
buttonTakePicture = (Button) findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        camera.takePicture(myShutterCallback, myPictureCallback_RAW,
            myPictureCallback_JPG);

现在按下takePicture按钮后,图像应该保存在sdcard中,它正在拍摄快照但没有保存图像,代码在下面

File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");
FileOutputStream fos;
try {
    imageFileOS = getContentResolver().openOutputStream(uriTarget);
    imageFileOS.write(arg0);
    imageFileOS.flush();
    imageFileOS.close();
    fos = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.close();
    Toast.makeText(AndroidCamera.this,
            "Image saved: " + uriTarget.toString(),
            Toast.LENGTH_LONG).show();

请帮我解决这个问题....提前致谢

2 个答案:

答案 0 :(得分:3)

您可以在Android中保存照片,而无需使用OutputStream及其附带的所有其他逻辑。这是一个简单的配方,可以做我认为你想要完成的事情。特别注意Intent以及它如何用于设置图像的保存,因为我认为这是你出错的地方。

public class PhotoActivity extends Activity implements OnClickListener {

private Button takePicture;
private String path;
private File imageFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    takePicture = (Button) findViewById(R.id.button1);
    takePicture.setOnClickListener(this);

    path = Environment.getExternalStorageDirectory() + "/my_image.png";
    imageFile = new File(path);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        Uri uri = Uri.fromFile(imageFile);
        i.putExtra(MediaStore.EXTRA_OUTPUT, uri);

        startActivity(i);
        break;
    }
}
}

答案 1 :(得分:1)

  private static final int CAMERA_REQUEST = 1888;  
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View arg0) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());

    startActivityForResult(intent, CAMERA_REQUEST);

                    }

            });

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == CAMERA_REQUEST) {

                    photo = (Bitmap) data.getExtras().get("data");



                }
    }

    private Uri getImageUri() {


            File file1 = new File(Environment.getExternalStorageDirectory() + "/Camerafolder");
            if (!file1.exists())
            {
                file1.mkdirs();
            }

            File file = new File(Environment.getExternalStorageDirectory() + "/Camerafolder/"+"img"+".png");

    Uri imgUri = Uri.fromFile(file);

            return imgUri;
        }