Ravi
Ravi

Reputation: 960

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

I am making an android application in which i have to select image from gallery by clicking a button and then display it in another activity with two text fields, the problem is i am able to open the gallery and select image from it but i am not able to display image in another activity... here is my code... PictureOptions.java

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

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

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

         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(PictureOptions.this,ShowImage.class);
         intent.putExtra("data", selectedphoto);
         startActivity(intent);
     }

PictureOptions.xml

<Button
    android:id="@+id/buttonGalleryOpen"
    android:layout_width="fill_parent"
    android:layout_height="66dp"
    android:layout_weight="0.34"
    android:onClick="buttonGalleryOpen"
    android:text="@string/button_gallery_open" />

ShowImage.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_image);
    ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
    Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
    imageview.setImageBitmap(selectedphoto);
}

ShowImage.xml

 <ImageView
    android:id="@+id/ImageShow"
    android:layout_width="200dp"
    android:layout_height="200dp" /> 

All things are working fine and second activity(ShowImage) is also opening except that no iamge is displying....dont know why..?HELP

Upvotes: 1

Views: 6401

Answers (3)

Vaishali Sutariya
Vaishali Sutariya

Reputation: 5121

private void selectImage() {
    final CharSequence[] items = { "Photo Library", "Camera", "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Select");
    Utils.hideSoftKeyboard(getActivity());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Camera")) {

                  // camera intent

            } else if (items[item].equals("Photo Library")) {

                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);

            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {

          if (requestCode == SELECT_FILE) {
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, getActivity());

            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bitmap = BitmapFactory.decodeFile(tempPath, btmapOptions);
            resized = Bitmap.createScaledBitmap(bitmap,
                    (int) (bitmap.getWidth() * 0.8),
                    (int) (bitmap.getHeight() * 0.8), true);

            profileEditImageView.setImageBitmap(resized);

     }
    }
   }


public String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaColumns.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = activity
            .managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Upvotes: 0

Okas
Okas

Reputation: 2664

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 inefficient, 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( uri );

In your ShowImage activity do:

URI imageUri = getIntent().getData();

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157447

Yo have a typo in intent.putExtra("data", "selectedphoto");, you are passing a String not the bitmap. Change it in

 Intent intent = new Intent(PictureOptions.this,ShowImage.class);
 intent.putExtra("data", selectedphoto);
 startActivity(intent);

removing the double quote from selectedphoto

Upvotes: 0

Related Questions