Select a photo from the gallery and show it in another Activity

Viewed 42

I am making an mobile app in which i have to select image from gallery by clicking a button and then display it in another activity. The problem is the image wont show to the second activity. Theres no problems running the code. what would be the problem.. PLEASE HELP ME

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

}

public void buttonGalleryOpen(View view)
{
Intent intent = new         Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    //noinspection deprecation
    startActivityForResult(intent, RESULT_OK);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Bitmap selectedphoto = null;


super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null){

Uri selectedImage = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null,     null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
selectedphoto = BitmapFactory.decodeFile(filePath);
 cursor.close();
Intent intent = new Intent(MainActivity.this,gallery_view.class);
intent.putExtra("data", selectedphoto);
startActivity(intent);
}
}
}

I am making an mobile app in which i have to select image from gallery by clicking a button and then display it in another activity. The problem is the image wont show to the second activity. Theres no problems running the code. what would be the problem.. PLEASE HELP ME

This is an 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">

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera sample"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_below="@+id/textview1"
android:layout_marginTop="3dp">

<Button
android:id="@+id/bottonGalleryOpen"
android:onClick="buttonGalleryOpen"

android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="pick" />

</RelativeLayout>


</RelativeLayout>

gallery_view.java

public class gallery_view extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_view);
ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
Bitmap selectedImage  =(Bitmap)this.getIntent().getParcelableExtra("data");
imageview.setImageBitmap(selectedImage);

}
}

gallery_view.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">

<RelativeLayout
android:id="@+id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera sample"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

<RelativeLayout
android:layout_below="@id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/ImageShow"
android:layout_width="match_parent"
android:layout_height="500dp" />
    
</RelativeLayout>

</RelativeLayout>
1 Answers

This line in your code does not make sense:

intent.putExtra("data", "selectedphoto");

You are adding here string "selectedphoto" which is in no way connected to selectedphoto variable you initialised earlier. You could put your bitmap to intent extra as byte array but this is in-efficient, especially when the image is large. Instead of passing bitmap to ShowImage activity, pass your URI and then retrieve actual bitmap in ShowImage activity exactly as you do now in your PictureOptions activity.

intent.setData(passed uri here);

In your ShowImage activity do:

URI imageUri = getIntent().getData();
Related