Low picture/image quality when capture from camera
Asked Answered
S

3

48

I have one problem. When I try to get picture from camera, the quality is very low. At first, capture the picture using camera, than save to the directory and at the same time get that picture and show in my app.The picture saved inside directory is a fine quality but when I get it from directory, the quality is low. below is my sample code :

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == CAMERA_PIC_REQUEST) {  

        Bitmap thumbnail = (Bitmap) intent.getExtras().get("data");

        if (g==1)
        {
            ImageView myImage = (ImageView) findViewById(R.id.img5);
            myImage.setImageBitmap(thumbnail);

            View a = findViewById(R.id.img5);
            a.setVisibility(View.VISIBLE);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, stream);

            byteArray1 = stream.toByteArray();
        }
}

any solution/suggestion? Thanks :)

Solved

The problem solved when I follow the code given by Antrromet below

Scotch answered 30/4, 2012 at 2:14 Comment(4)
I don't see you getting the photo from the directory anywhere? All I see is you use the bundled thumbnail, which, by the way, is prone to errors as in my experience not all devices actually return any "data" extra. The thumbnail will obviously not have the same size as the 'full' picture on your storage medium.Venerable
actually I call thecamera using this: Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); what is your suggestion then to avoid the error?and how to get the actual size of picture? Thanks for your reply :)Scotch
Best approach is probably to supply the output path with the Camera intent. Alternatively you could also implement your own photo capture logic (more work), or possibly use a FileObserver on the DCIM/Camera directory.Venerable
Thanks for explaining.now I understand how its work :)Scotch
E
106

I have used the following code and this works perfectly fine for me.

            values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, "New Picture");
            values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, PICTURE_RESULT);

and also

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

        case PICTURE_RESULT:
            if (requestCode == PICTURE_RESULT)
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        thumbnail = MediaStore.Images.Media.getBitmap(
                                getContentResolver(), imageUri);
                        imgView.setImageBitmap(thumbnail);
                        imageurl = getRealPathFromURI(imageUri);    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
        }
    }

and

public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
Esbenshade answered 30/4, 2012 at 10:33 Comment(10)
The above code works but somehow in OnActivityResult() -> the imageUri is shown null sometimes even though I've set imageUri while starting CameraActivity. @EsbenshadeDennadennard
^ [SOLVED] -> declared the imageUri and ContentValues in the onCreate() method rather than onClick listener :DDennadennard
cannot understand the use of - imageurl = getRealPathFromURI(imageUri); ?Burch
Not working in Android 7.0. Checkout this: developer.android.com/training/camera/photobasics.htmlChoroid
@Esbenshade any solution for Android N7.0?Hereabout
it takes too much time as the images size goes upto 14mbTodhunter
It is recommended to use this function "getRealPathFromURI" on Ui Thread, because it is a query to media store database. It might cause an ANR.Serial
Anyone can enlighten me about the imageurl ? It is used as imageurl = getRealPathFromURI(imageUri); but not used anywhere elseDoodlesack
Working!! ThanksPoultryman
Whats imageLocationUri = getRealPathFromURI(imageLocationUri);Camarata
O
5

I will give you something that work for me. I asked the same !

Solution to save a picture in a specific folder without lost quality

I FOUND THE SOLUTION:

//Create a new folder code:

 String path = String.valueOf(Environment.getExternalStorageDirectory()) + "/your_name_folder";
            try {
                File ruta_sd = new File(path);
                File folder = new File(ruta_sd.getAbsolutePath(), nameFol);
                boolean success = true;
                if (!folder.exists()) {
                    success = folder.mkdir();
                }
                if (success) {
                    Toast.makeText(MainActivity.this, "Carpeta Creada...", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception ex) {
                Log.e("Carpetas", "Error al crear Carpeta a tarjeta SD");
            }

            Intent i = new Intent(MainActivity.this, MainActivity.class);
            startActivity(i);
            finish();//agregue este

//Method to take a Picture

public void clickFoto(View view) {
        takePic();
    }

//takePic()

 private void takePic() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        *File file = new File(Environment.getExternalStorageDirectory(), "/your_name_folder/a" + "/photo_" + timeStamp + ".png");*
        imageUri = Uri.fromFile(file);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, PICTURE_RESULT);
    }

//onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case PICTURE_RESULT:
                if (requestCode == PICTURE_RESULT)
                    if (resultCode == Activity.RESULT_OK) {
                        try {
                            Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                                    getContentResolver(), imageUri);
                            imgFoto.setImageBitmap(thumbnail);
                            imageurl = getRealPathFromURI(imageUri);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
        }
    }

// getRealPathFromUri

public String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
Onestep answered 20/4, 2016 at 16:30 Comment(1)
In takePic() you accidentally added an asterisk and also u forgot Uri imageUri;.Gignac
P
-1

Add the photo to a gallery . .

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(currentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

for more detail : https://developer.android.com/training/camera/photobasics.html

Propitiate answered 14/4, 2020 at 5:10 Comment(1)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MEDIA_SCANNER_SCAN_FILE }Miltonmilty

© 2022 - 2024 — McMap. All rights reserved.