How to compress and reduce size(resolution) of image file from your Android App? - Android 13 API 33

Описание к видео How to compress and reduce size(resolution) of image file from your Android App? - Android 13 API 33

This video shows the steps to compress the image size and save it as another file in the download folder from your Android App.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...


However, the main Java code is copied below also for reference:

package com.programmerworld.compressmyimage;

import static android.Manifest.permission.READ_MEDIA_IMAGES;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
private ImageView imageView, imageView2;
private StorageVolume storageVolume;
private Bitmap bitmapImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this,
new String[]{READ_MEDIA_IMAGES, WRITE_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);

imageView = findViewById(R.id.imageView);
imageView2 = findViewById(R.id.imageView2);

StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
storageVolume = storageManager.getStorageVolumes().get(0); // internal storage
File fileInput = new File(storageVolume.getDirectory().getPath() + "/Download/images.jpeg");
bitmapImage = BitmapFactory.decodeFile(fileInput.getPath());

imageView.setImageBitmap(bitmapImage);
}

public void buttonCompressImage(View view) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 0, byteArrayOutputStream);
byte[] bytesArray = byteArrayOutputStream.toByteArray();
Bitmap bitmapCompressedImage = BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.length);
imageView2.setImageBitmap(bitmapCompressedImage);

File fileOutput = new File(storageVolume.getDirectory().getPath() + "/Download/output1.jpeg");
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
fileOutputStream.write(bytesArray);
fileOutputStream.close();
}
}

--

Комментарии

Информация по комментариям в разработке