Android how to open image and display it in second activity

androidandroid-galleryandroid-imageview

I am working on app for editing photos

I have a button in first activity and ImageView in second activity. When I click the button it would open gallery and I would be able to select an image. The selected image needs to appear in my ImageView in second activity but it doesn't.

Down below is my code which is not working,have you any idea what is wrong?

FIRST ACTIVITY:

package com.example.odabirslike;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Pocetni extends Activity {

    private Button buttonLoadImage;

    private static final int SELECT_PICTURE = 1;

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

        this.buttonLoadImage = (Button) this.findViewById(R.id.pickbutton);

        buttonLoadImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
            {
                Intent i = new Intent();
                  i.setType("image/*");
                  i.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(i, "Complete action using"), SELECT_PICTURE);               
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
         Bitmap selectedphoto   = null;
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_PICTURE && 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 i = new Intent (Pocetni.this, Drugi.class);
            i.putExtra("data",selectedphoto);
            startActivity(i); 
        }

    }
}

SECOND ACTIVITY:

package com.example.odabirslike;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.widget.ImageView;

public class Drugi extends Activity {

    ImageView view = (ImageView) findViewById(R.id.imageView1);

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

        Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
        view.setImageBitmap(selectedphoto);
    }
}

ANDROID MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.odabirslike"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.odabirslike.Pocetni"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.odabirslike.Drugi"
            android:label="Drugi" >
        </activity>
    </application>

</manifest>

Best Answer

It might be that the Bitmap is too big to be passed around like that. Why don't you instead, put

 String filePath = cursor.getString(columnIndex);
 i.putExtra("data",filepath);

in your first activity. And in your second

String filepath   =this.getIntent().getStringExtra("data");
Bitmap selectedphoto = BitmapFactory.decodeFile(filePath);

Should be less cumbersome for your app, at least