deveLost
deveLost

Reputation: 1171

Android photo in imageview, bad quality

I allow user to take photo, and I get this photo to set it in a imageview.

This is my code for my imageview :

        <ImageView 
        android:id="@+id/photo1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:layout_marginBottom="2dp" />

And my code to put the photo in this imageview :

        imgPhoto1.setImageBitmap(btmap);

My problem is, the photo is showing is blurred.....

How can I do to have a "correct" quality ?

I tried to use this :

getWindow().setFormat(PixelFormat.RGBA_8888);

but it changes nothing.

Thx,

EDIT : Please, I'm searching, if possible, a solution without to use a library, it's just for two imageview..

EDIT 2 : Ok, it seems impossible to do without library, because my image take all of space available on screen.

Upvotes: 2

Views: 10896

Answers (5)

Guorti
Guorti

Reputation: 1

You want to obtain the URI of the photo, not the bitmap preview. Outside your onCreate()

private lateinit var photoUri: Uri

Inside the check of camera permission. It worked using this function for taking the picture

private fun takePic() {
    val permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
    val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        if (takePictureIntent.resolveActivity(packageManager) != null) {
            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                ex.printStackTrace()
                null
            }
            photoFile?.also {
                photoUri = FileProvider.getUriForFile(
                    this,
                    "com.example.myapp.fileprovider",
                    it
                )
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
                startActivityForResult(takePictureIntent, Permission.REQUEST_IMAGE_CAPTURE)
            }
        }
    }
}

Function to generate an image file and retrieve its URI.

private fun createImageFile(): File {
    // Create an image file name
    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    return File.createTempFile(
        "JPEG_${timeStamp}_", 
        ".jpg",
        storageDir
    )
}

On your onActivityResult()

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when(requestCode){
        Permission.REQUEST_IMAGE_CAPTURE -> {
            if (resultCode == Activity.RESULT_OK) {
                // Load the full-quality image into ImageView
                val imageView = findViewById<ImageView>(R.id.photo)
                imageView.setImageURI(photoUri)
            }
        }
    }
}

Modify your AndroidManifest.xml and add inside <application

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.myapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

And create on your res/xml an xml file named file_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures/"/>

Upvotes: 0

Taras Okunev
Taras Okunev

Reputation: 410

This function resolves the problem of bad quality of a image at ImageView:

public static Bitmap getBitmapFromResources(Resources resources, int resImage) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = 1;
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    return BitmapFactory.decodeResource(resources, resImage, options);
}

And imageView.setImageBitmap(getBitmapFromResources(getResources(), R.drawable.image));

Upvotes: 3

Patato
Patato

Reputation: 1472

change your imgview width and height style

<ImageView 
        android:id="@+id/photo1"
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:layout_marginBottom="2dp" />

Hope It help

Upvotes: 0

Warwicky
Warwicky

Reputation: 281

You can also use AndroidQuery. It also allows you to load images async. I never had a quality issue with it. It is pretty easy to use and also allows you to cache images.

https://code.google.com/p/android-query/

Upvotes: 0

VINIL SATRASALA
VINIL SATRASALA

Reputation: 634

Instead of loading bitmap use universal image loader or picasso for this:

Android-Universal-Image-Loader

picasso

Why use Android Picasso library to download images?

Upvotes: 0

Related Questions