1

I'm completely news on android thing and unfortunately with little few time to learn it by the right way, I have a work to release. The problem is: I need to take a picture and process her with an algorithm that I made. I did it by the easiest way that I could find, I know it looks like really trahsie for those who really get android (sorry)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    takePic();

protected void takePic(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, 100);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Bundle extras = data.getExtras();
     mImageBitmap = (Bitmap) extras.get("data");
             Algorithm(mImageBitmap)

But it doesn't process, it takes a photo, ask to save or cancell and leaves the application, I have already made by different ways (creating a new activity), but nothing seems to work

  • what does your Algorithm(..) method do? I'm going to assume that is crashing – dymmeh Jul 24 '13 at 17:10
  • this might help - http://stackoverflow.com/questions/15248265/camera-intent-not-working-with-samsung-galaxy-s3/15287164#15287164 – Carbon Jul 24 '13 at 18:03

1 Answers1

0

Heres how I did it

To go to the camera:

Somewhere, declaire a fileUri variable and hold onto it

Uri fileUri;
final int TAKE_PICTURE=100;//this can be any int, really
public void goToCamera(){
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo;
    try
    {
        // place where to store camera taken picture
        photo = this.createTemporaryFile("picture", ".jpg");
        Log.v(TAG, "Here(after createTempFile)");
        photo.delete();
    }
    catch(Exception e)
    {
       Log.v(TAG, "Can't create file to take picture!" + e.getMessage());
       Toast.makeText(context, "Please check SD card!", Toast.LENGTH_SHORT).show();
       return; 
    }
    fileUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
    startActivityForResult(intent, TAKE_PICTURE);
}

Then to retreive the image

protected void onActivityResult(int requestCode, int resultCode, Intent data){

    if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK){
       this.getContentResolver().notifyChange(uri, null);
       ContentResolver cr = this.getContentResolver();
       Bitmap bitmap;
       try
       {
           BitmapFactory.Options ops = new BitmapFactory.Options();
           ops.inSampleSize = 4;
           bitmap = BitmapFactory.decodeFile(uri.getPath().toString(), ops);
       }
       catch (Exception e)
       {
           Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
           Log.d(TAG, "Failed to load", e);
       }
    }
}

The create temp file mentioned above:

private File createTemporaryFile(String part, String ext) throws Exception
{
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
    Log.i(TAG, tempDir.toString());
    if(!tempDir.exists())
    {
        Log.i(TAG, "Dir doesnt exist");
        tempDir.mkdirs();
    }
    return File.createTempFile(part, ext, tempDir);
}

I realize this isn't probably as simple as you were hoping for, but this approach seemed to be as flexible and compatible as possible. Let me know if I left anything else out

Jameo
  • 4,507
  • 8
  • 40
  • 66