Android 照片在图像视图中,质量很差

问题描述 投票:0回答:5
android image photo
5个回答
3
投票

此功能解决了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);
}

还有

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


0
投票

使用通用图像加载器或 picasso 代替加载位图:

Android 通用图像加载器

毕加索

为什么要使用Android Picasso库下载图片?


0
投票

您还可以使用AndroidQuery。它还允许您异步加载图像。我从来没有遇到过质量问题。它非常易于使用,还允许您缓存图像。

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


0
投票

更改你的imgview宽度和高度样式

<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" />

希望有帮助


0
投票

您想要获取照片的 URI,而不是位图预览。 在 onCreate() 之外

private lateinit var photoUri: Uri

在相机权限检查中。使用这个功能拍照就可以了

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

生成图像文件并检索其 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
    )
}

在你的 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)
            }
        }
    }
}

修改你的AndroidManifest.xml并在里面添加

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

并在 res/xml 上创建一个名为 file_paths.xml 的 xml 文件

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures/"/>
© www.soinside.com 2019 - 2024. All rights reserved.