开发者

Saving pictures from the camera in Android

You'll have to bear with me, I'm very new to Android and my Java skills are "rusty" at best.

I was pretty excited to get the barcode scan bit working with very little effort indeed, but the camera side of things really isn't proving as easy.

I've got a main Activity class that calls for a barcode when it starts up;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button cameraButton = (Button) findViewById(R.id.cameraButton);
    cameraButton.setOnClickListener( new OnClickListener(){
        public void onClick(View v ){
            IntentIntegrator.initiateScan(MakroDroidActivity.this, "Please scan order barcode", "Please scan a valid order barcode", "Yes", "No");
        }
    });
}

Working so far and I correctly handle the intent as shown:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
            switch(requestCode) { 
                case IntentIntegrator.REQUEST_CODE: { 
                    if (resultCode != RESULT_CANCELED) { 
                        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
                        if (scanResult != null) { 
                            String upc = scanResult.getContents();
                            if (upc.startsWith("ORD"))
                     开发者_StackOverflow社区       {
                                //we have a valid order barcode
                                Camera cam = Camera.open();
                                cam.takePicture(shutterCallback, rawCallback, jpegCallback); 
                            }
                            else
                            {
                                IntentIntegrator.initiateScan(MakroDroidActivity.this, "Please scan again", "Please scan a valid order barcode", "Yes", "No"); 
                            }
                        } 
                    } 
                    break; 
                } 
            } 
        }

I have the call backs set up (currently with no code in them) but after the barcode is scanned, the device just displays a black screen. I then need to restart the device after debugging the app because the camera is no longer available (I suspect that I have not cleaned up my reference so the camera is locked to my app)

Can somebody explain to me how I go about using the camera and saving the output to the SD card (I'm planning on using a specific folder on the SD card with something like:

filepath = filepath + UUID.randomUUID().toString() + ".jpg"; 

to save a new, non-conflicting filename to the folder.

(Device is a T-Mobile pulse mini (Huawei manufactured I believe) and I have seen other people say there are driver issues with the camera but I am more inclined to believe it is a lack of my understanding rather than a hardware issue.)

Thanks in advance for any assistance you can give on this.


try this out:

public void takePhoto()
    {
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
    startActivityForResult(intent, 1);
    }

private File getTempFile(Context context)
    {
    File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName() );
    if(!path.exists())
        path.mkdir();
    return new File(path, "debris.jpg");
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    if (resultCode == RESULT_OK)
        {
        switch(requestCode)
            {
            case 1:
            final File file = getTempFile(this);
            try
                {
                Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
                image_string = Uri.fromFile(file).toString();
                }
            catch (FileNotFoundException e)
                {
                Toast.makeText(getApplicationContext(), "file not found exception", Toast.LENGTH_SHORT).show();
                }
            catch (IOException e)
                {
                Toast.makeText(getApplicationContext(), "ioexception", Toast.LENGTH_SHORT).show();
                }
            break;
            }
        }
    }
}

it could be smaller and cleaner than that but this does all the things you asked

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜