简体   繁体   English

如何调整图库中的高质量图像的尺寸以使其出现在图像视图中?

[英]How to resize high quality image from gallery to appear in image view?

in my app when i open gallery and pic an image it appears but when this image is high quality the image view show nothing also in another phone the image from camera appear horizontally by it self 在我的应用程序中,当我打开图库并为图像拍照时,它会出现,但是当此图像质量很高时,图像视图也不会显示任何东西,而在另一部手机中,来自相机的图像也会自动出现

class Horizontal : Fragment() , EasyPermissions.PermissionCallbacks{

    private var currentImage: Bitmap? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? { val view =inflater.inflate(R.layout.horizontal, container, false)

        val btng = view.findViewById<View>(R.id.btn_gallery_Hz) as FloatingActionButton

        btng.setOnClickListener {openGallary()
            selectImageInAlbum()}
        btng.scaleType = ImageView.ScaleType.CENTER
        return view}

    private fun selectImageInAlbum() {
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.type = "image/*"
        if (intent.resolveActivity(activity!!.packageManager) != null) {
            startActivityForResult(intent, 1} }




    override fun onStop() {
        super.onStop();if (currentImage != null)
        {currentImage!!.recycle();currentImage = null;System.gc()}}



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==2&&resultCode == Activity.RESULT_OK && null != data) {
            val selectedImages = data.data
            val filePathColon = arrayOf(MediaStore.Images.Media.DATA)
            val cursr = this.activity?.contentResolver?.query(selectedImages!!,
                filePathColon, null, null, null)
            cursr!!.moveToFirst()
            val columnindex = cursr.getColumnIndex(filePathColon[0])
            val picturepath = cursr.getString(columnindex)
            cursr.close()
            val intent = Intent(activity, ResHzL::class.java)
            intent.putExtra("asd", picturepath)
            startActivity(intent)}}




    @AfterPermissionGranted(2)
    private fun openGallary() {
        val perms = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE)
        if (EasyPermissions.hasPermissions(this.activity!!, *perms)) {

            // Toast.makeText(activity, "Opening gallary", Toast.LENGTH_SHORT)
        } else {EasyPermissions.requestPermissions(this,
            getString(R.string.weneedper), 2, *perms) }}
    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<String>, grantResults: IntArray
    ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)}
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            AppSettingsDialog.Builder(this).build().show()}}
    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {}}

And in the other activity i use this to show the image 在其他活动中,我用它来显示图像

val imageView = findViewById<View>(R.id.mainImg_Hz) as ImageView
        imageView.setImageBitmap(BitmapFactory.decodeFile(intent.getStringExtra("asd")))

Can any one could help ? 有人可以帮忙吗?

First of all you need to process this image so that you can reduce the size but quality you can maintain. 首先,您需要处理此图像,以便减小尺寸并保持质量。 You need to run a background task so that during big image process device doesn't fizz. 您需要运行后台任务,以便在大图像处理过程中设备不会起毛。

Then you can show a progress dialog during this process just add this code into your onCreate activity. 然后,您可以在此过程中显示一个进度对话框,只需将此代码添加到onCreate活动中即可。

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

progressDialog = new ProgressDialog(MyProfileEidtActivity.this);
progressDialog.setMessage("Loading ...");


// just execute this process

new ImageProcessing().execute("YOUR IMAGE PATH");
}


public class ImageProcessing extends AsyncTask<String, Void, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog.setMessage("Image Processing");
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
protected String doInBackground(String... strings) {
    Bitmap mainImage = null;
    Bitmap converetdImage = null;
    ByteArrayOutputStream bos = null;
    byte[] bt = null;
    String encodeString = null;
    try {
        mainImage = BitmapFactory.decodeFile(strings[0]);

    /// 500 means image size will be maximum 500 kb

        converetdImage = getResizedBitmap(mainImage, 500);
        bos = new ByteArrayOutputStream();
        converetdImage.compress(Bitmap.CompressFormat.JPEG, 50, bos);
        bt = bos.toByteArray();
        encodeString = Base64.encodeToString(bt, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encodeString;
 }

@Override
protected void onPostExecute(String image) {
    super.onPostExecute(s);
    progressDialog.dismiss();

 // this image will be your reduced image path

}
 }

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();

float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
    width = maxSize;
    height = (int) (width / bitmapRatio);
} else {
    height = maxSize;
    width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM