Saving camera photos in Android

You will have to carry with me, I am very new to Android, and my Java skills are "rusty" at best.

I was very happy to get a barcode scan bit that works with very little effort, but the camera side is not very simple.

I have a core Activity class that calls a barcode when it starts;

/** 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");
        }
    });
}

We are still working, and I correctly process the intention, 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"))
                            {
                                //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 configured callbacks (they currently have no code), but after scanning the barcode, the device simply displays a black screen. Then I have to restart the device after debugging the application, because the camera is no longer accessible (I suspect that I have not cleared my link, so the camera is locked for my application)

- , SD- ( SD- - :

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

, .

( - - T-Mobile ( , Huawei), , , , , , .)

, .

+3
1

:

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

, , ,

0

Source: https://habr.com/ru/post/1753495/


All Articles