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: programmerworld1990@gmail.com
Complete source code and other details:
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();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.CompressMyImage"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="114dp"
android:layout_marginTop="47dp"
android:onClick="buttonCompressImage"
android:text="Compress Image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="180dp"
android:layout_height="165dp"
android:layout_marginStart="112dp"
android:layout_marginTop="68dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:srcCompat="@drawable/ic_launcher_background" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="180dp"
android:layout_height="165dp"
android:layout_marginStart="96dp"
android:layout_marginTop="88dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>