Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to implement an app that takes pics from native camera and display the result in an imageView. the code that i used is very inconsistent. when i run the app, it sometimes shows the image in the imageView else doesn't show the result at all.

the code that i used is as follows:

this function opens the camera app
Java
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        // Error occurred while creating the File
                        Log.i(TAG, "IOException");
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
                    }
                }


this function is used to save the image the the device memory
Java
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  // prefix
                ".jpg",         // suffix
                storageDir      // directory
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;


Java
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            try {
                if (mImageBitmap == null) {
                    Toast.makeText(getApplicationContext(), "Unable to Process. Please adjust camera Resolution", Toast.LENGTH_SHORT).show();
                } else {
                    mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
                    imageView.setImageBitmap(mImageBitmap);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
Posted
Comments
Richard MacCutchan 29-Dec-15 7:05am    
We need more information on this. Try doing some debugging to find what is different when you get a failure. Also, when catching exceptions you should log all the details, not just the name of the exception.
wseng 29-Dec-15 11:07am    
If the selected image is more than 1MB, it will not showing. The better way is scale the selected image. Please correct me if I'm wrong
Rahul Ramakrishnan 29-Dec-15 22:31pm    
i'll give it a try
wseng 30-Aug-16 11:06am    
Have you fixed it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900