Save Image from drawable to sdcard in camera activ

2019-06-14 11:03发布

问题:

I'm working on a project, that read image file from drawable folder through ImageView, it loaded successfully through

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);

now after pressing takePicture button , image should save in sdcard, it is taking snapshot but not saving the image with that, the code is below

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();

Please help me for this issue....Thanks in advance

回答1:

You can save photos in Android without the need for an OutputStream and all the other logic that accompanies it. Here is a simple recipe which will do what I think you are trying to accomplish. Pay special attention to the Intent and how it is used to set up the saving of the image, as this is I think where you are going wrong.

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;
    }
}
}


回答2:

  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;
        }