Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

java - saving pictures taken by camera in android app

I have been stuck on this for a while now and have looked at various tutorials for help but have not yet succeeded.

I have essentially utilised the camera function in my app to take pictures and display a preview of it BUT it can't save taken picture.

Here is the java code containing my attempt to get it functioning according to tutorials:

public class Activity_Camera extends Activity implements View.OnClickListener {

    ImageButton ib;
    ImageView iv;
    Intent i;
    public static final int cameraData = 0;
    Bitmap bmp;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.Info:

            Intent i = new Intent(this, Help.class);
            startActivity(i);

            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initialise();
    }

    private void initialise() {
        iv = (ImageView) findViewById(R.id.ivPicReturn);
        ib = (ImageButton) findViewById(R.id.ibTakePic);

        ib.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ibTakePic:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameraData);
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            iv.setImageBitmap(bmp);

            Toast.makeText(this, "Image saved to:
" +
                    data.getData(), Toast.LENGTH_LONG).show();
       } else if (resultCode == RESULT_CANCELED) {
           // User cancelled the image capture
       } else {
           // Image capture failed, advise user
       }
    }

    private static Uri getOutputMediaFileUri(int type){
          return Uri.fromFile(getOutputMediaFile(type));
    }
    private static File getOutputMediaFile(int type){

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");

        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        File mediaFile;
        if (type == cameraData){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_" + ".jpg");
        } else {
            return null;
        }
        return mediaFile;
    }
}

I have already included all the neccessary permissions within the Manifest.xml file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();   
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

Have a look at this link.How to save images from Camera in Android to specific folder?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...