2

I used code from http://developer.android.com/guide/topics/media/camera.html#custom-camera but I have a problem with PictureCallback in this line

File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

The problem is "MEDIA_TYPE_IMAGE cannot resolved to a variable". How can I solve this problem?

Oh Ho
  • 21
  • 1
  • 2

3 Answers3

3

if the camera example from the android guide led you here then read this http://developer.android.com/guide/topics/media/camera.html#saving-media they have declared the method at the bottom of the documentation, threw me off track too, Hope it helped you

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
1

getOutputMediaFile () this method created by developer site to save captured image to make it run complete your code paste this functions to your activity

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
    return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MTN_Camera");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}
shayah
  • 39
  • 7
0

You can check the following code##

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
public static final int MEDIA_TYPE_IMAGE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
Uri fileUri ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bt=(Button)findViewById(R.id.button1);

    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri=  getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        }
    });


}
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
private static File getOutputMediaFile(int type){

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

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

        TextView tv=(TextView)findViewById(R.id.textView2);
        if (resultCode == RESULT_OK) {

            tv.setText("Image saved to:\n"+data.getData());
            ImageView img=(ImageView)findViewById(R.id.imageView1);
            img.setImageURI(fileUri);
            //tv.setText(fileUri.toString());
        } else if (resultCode == RESULT_CANCELED) {
              tv.setText("Cancelled");
        } else {
            // Image capture failed, advise user
             tv.setText("Can con be captured");
        }
    }
}
}

You can also check here for more

You must use permission for external storage in manifest file

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Community
  • 1
  • 1
Mahbub
  • 4,812
  • 1
  • 31
  • 34