Picture Taken with Camera Intent Is Low Quality on Imageview (7.0+)

Picture taken with camera intent is low quality on ImageView (7.0+)

If you are asking "how do I get a full-resolution image?", supply EXTRA_OUTPUT on the ACTION_IMAGE_CAPTURE Intent, then look at that location for the image when onActivityResult() is called with RESULT_OK for your request.

This sample app illustrates the technique, using FileProvider for better compatibility on Android 7.0+:

/***
Copyright (c) 2008-2017 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

Covered in detail in the book _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.camcon;

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import java.io.File;
import java.util.List;

public class MainActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private static final String AUTHORITY=
BuildConfig.APPLICATION_ID+".provider";
private static final String PHOTOS="photos";
private File output=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState==null) {
output=new File(new File(getFilesDir(), PHOTOS), FILENAME);

if (output.exists()) {
output.delete();
}
else {
output.getParentFile().mkdirs();
}

Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip=
ClipData.newUri(getContentResolver(), "A photo", outputUri);

i.setClipData(clip);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}

try {
startActivityForResult(i, CONTENT_REQUEST);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.msg_no_camera, Toast.LENGTH_LONG).show();
finish();
}
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putSerializable(EXTRA_FILENAME, output);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

i.setDataAndType(outputUri, "image/jpeg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try {
startActivity(i);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.msg_no_viewer, Toast.LENGTH_LONG).show();
}

finish();
}
}
}
}

Low picture/image quality when capture from camera

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

How to set the size and quality of a picture taken with the android camera?

Check this answer here.

A camera intent return a thumbnail by default, so you need to
fetch the full image.

Derek's solution is right, but you have to improve it.

the real juice lies in fetching the full image which happens here -

thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
imgView.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);

full path will be given by getRealPathFromURI().

Resolution of image from camera to Imageview

In android the default data which you get from camera is low resolution thumbnail image.

So before you call CameraIntent create a file and uri based on that filepath as shown here.

filename = Environment.getExternalStorageDirectory().getPath() + "/folder/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));

// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult (cameraIntent, CAMERA_REQUEST);

Now, you have the filepath you can use it in onAcityvityResult method as following,you can also get the bitmap from the uri.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
ImageView img = (ImageView) findViewById(R.id.image);
img.setImageURI(imageUri);
}
}

How to take image from camera show in ImageView without losing its Quality in Android?

If you need your image in best quality, you need to save it to a file, otherwise you can just access the thumbnail (low res image). I will share how I did it.

Inside your manifest, you need to define a file provider inside application tag, permissions also.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application...>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="{your package name}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
</provider>
</application>

You will also need to make a FileProvider in your res/xml folder. Mine looks like this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="/storage/emulated/0/Pictures/" path="Pictures/"/>
</paths>

Please do more research, where do you want to save your photos, in my example you can save them only to SD card, but there are more options.

Now, taking photo and saving it.

private var currentPhotoUri = Uri.EMPTY
private var currentPhotoPath
private val requestCode = 123

private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(activity?.packageManager!!)?.also {

createFile()

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, currentFileUri)
startActivityForResult(takePictureIntent, requestCode)
}
}
}

private fun createFile(): File? {
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
Log.i("mylog error", "Error creating file: " + ex.toString())
ex.printStackTrace()
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = context?.let { context ->
FileProvider.getUriForFile(
context,
"{your package name}.fileprovider",
it
)
} as Uri
currentPhotoUri = photoURI

}
return photoFile
}

private fun createImageFile(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File? = context!!.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
Log.i("mylog", "storage dir = " + storageDir?.absolutePath)
return File.createTempFile(
"JPEG_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
}

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent)
{
if (requestCode == requestCode && resultCode == RESULT_OK) {
val bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),currentPhotoUri)
val imageView = findViewById(R.id.Image)
imageView.setImageBitmap(bitmap)
}
}

That should work. Dont forget to check permissions. You can learn more here.



Related Topics



Leave a reply



Submit