An activity has an icon to choose Image from Files. After choosing the Image, The next activity page opens which has A button and an ImageView where the selected iimage should be displayed.
I can choose the Image but it does not show on ImageView. I has tried Bitmap and it doesnt work. If I try to resize map and then run app, after choosing the image the app closes.
Below is the XML and Java of the activity
<?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=".ShowPhotoActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="196dp"
android:layout_height="219dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.414"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/btnEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT PHOTO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.773" />
</androidx.constraintlayout.widget.ConstraintLayout>
//Java of the Activity.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.widget.ImageView;
import java.io.File;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.File;
public class ShowPhotoActivity extends AppCompatActivity {
ImageView myImage;
Button btnEdit;
String path = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_photo);
myImage = (ImageView) findViewById(R.id.imageView);
btnEdit = findViewById(R.id.btnEdit);
path = getIntent().getExtras().getString("path");
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap b = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//myImage.setImageBitmap(Bitmap.createScaledBitmap(b, 40, 40, false));
myImage.setImageBitmap(b);
btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ShowPhotoActivity.this, EditPhotoActivity.class);
intent.putExtra("path", path);
startActivity(intent);
}
});
}
}
public static Bitmap RotateBitmat(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
}
I need the selected Image to be shown in the ImageView and when I click the Edit button the Image should be passed to the next activity too.