Code Tutorial #002

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<ImageButton
android:layout_width="130dp"
android:layout_height="130dp"
android:id="@+id/btn_pic"
android:src="@drawable/ic_launcher_foreground"
android:layout_centerInParent="true"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_pic"
android:id="@+id/btn_save"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_save"
android:id="@+id/btn_load"/>

</RelativeLayout>

 

 

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageActivity;
import com.theartofdev.edmodo.cropper.CropImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ImageButton btnPic;
Button btnSave, btnLoad;

private static final int PERMISSION_CODE = 1000;

Bitmap bitmap;
Uri imageUri;
File imageDirectory;

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

ContextWrapper cw = new ContextWrapper(getApplicationContext());
imageDirectory = cw.getDir("imageDir", MODE_PRIVATE);

btnPic = findViewById(R.id.btn_pic);
btnSave = findViewById(R.id.btn_save);
btnLoad = findViewById(R.id.btn_load);

btnPic.setOnClickListener(this);
btnSave.setOnClickListener(this);
btnLoad.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_pic:
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
{
try {
String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
requestPermissions(permissions, PERMISSION_CODE);
} catch (Exception e)
{

}
}
else
{
pickImageFromGallery();
}
}
break;
}
case R.id.btn_save:
{
if(imageUri == null)
{
Toast.makeText(this, "Please select a player and an image for that player", Toast.LENGTH_SHORT).show();
}
else
{
String name = "Picture.png";
Toast.makeText(this, "Picture is saved", Toast.LENGTH_SHORT).show();
saveImage(name);
}
break;
}
case R.id.btn_load:
{
File existingImage = new File(imageDirectory, "Picture.png");
try {
bitmap = BitmapFactory.decodeStream(new FileInputStream(existingImage));
btnPic.setImageBitmap(Bitmap.createScaledBitmap(bitmap, btnPic.getWidth(), btnPic.getHeight(), false));
} catch (Exception e)
{

}
break;
}
}
}

private void saveImage(String name) {
File myPath = new File(imageDirectory, name);
FileOutputStream fos = null;

try{
fos = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch(Exception e)
{

} finally {
try {
fos.close();
} catch (Exception e)
{

}
}
}

private void pickImageFromGallery() {
CropImage.startPickImageActivity(this);
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
imageUri = CropImage.getPickImageResultUri(this, data);
cropRequest();
}

if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK)
{
try{
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), result.getUri());
btnPic.setImageBitmap(Bitmap.createScaledBitmap(bitmap, btnPic.getWidth(), btnPic.getHeight(), false));
} catch (Exception e)
{

}
}
}
}

private void cropRequest() {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setMultiTouchEnabled(true)
.setAspectRatio(1,1)
.start(this);
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *