2

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>
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
Toro_master
  • 21
  • 1
  • 2

2 Answers2

0

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

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • I tryed to put this part of code but still doesn't work. And now I god error "source not found" – Toro_master May 10 '13 at 13:24
  • Could you please post your logcat? And while you're at it: what is the content of filepath when you invoke i.putExtra("data",filepath);? – DigCamara May 10 '13 at 14:42
  • Filepath content should be uri from chosen photo. Problem starts when I choose photo from gallery.I can click on a button,than I can open gallery and pick some photo,then Debugger displays that message My logcat is really to big,should I post some specific part or problem? – Toro_master May 11 '13 at 13:27
  • You're saying that once you choose a photo Android displays "source not found"? Also: what do you mean with "Filepath content should be uri from chosen photo"? What value **are** you getting? I'm asking because you should be able to navigate that filepath to see if the file is there. – DigCamara May 11 '13 at 14:33
  • Yes,emulator runs ok,until I pick some photo,then debugger display this message.Sorry but I'm not really sure what do you think about when you said "What value are you getting?" – Toro_master May 14 '13 at 13:55
  • I would like to know what value filepath has (what String it contains) both when you put it into the intent and when you are extracting it – DigCamara May 14 '13 at 14:34
  • Well,I think that there is no problem with this part of code,I have found it on this page(http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app-in-android),and it works when I want do display image in first activity,but I really need to pass it to another activity,this is my problem. – Toro_master May 14 '13 at 20:50
  • A quick glance tells me that the code from that answer should server you very well, actually. Why don't you just use that code in the second activity? – DigCamara May 14 '13 at 20:53
  • Because I'm trying to make app in which I need to have first activity whith two buttons,one for camera and one for gallery,and when I pick some photo it should be displayed in second activity.This code is just part of it. – Toro_master May 15 '13 at 07:07
  • Well, I guess I'll have to leave this question then (I'm not really understanding where your problems are, apparently). Good luck! – DigCamara May 15 '13 at 12:06
  • I have a problem because this picture doesn't appear in second activity.Ok if you can't help me,thank you for your effort. – Toro_master May 15 '13 at 20:29
0
    public class Drugi extends Activity {

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

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

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

Try that. but, replace sending data through activities, send the filename instead as suggested before.