• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android app closes after choosing picture, why?

Lewis99

Newbie
Hello says in title really. When i click button "Add profile picture" it goes onto gallery as it should but once i click a picrture it collapse and the app closes, why is this? I have an image view that is what is goes on?

package com.example.timetable;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

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

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String picturePath = prefs.getString("picture", "");
if(!picturePath.equals("true")){
ImageView imageView = (ImageView) findViewById(R.id.picture);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
{
}

Button buttonLoadImage = (Button) findViewById(R.id.picturebutton);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});;


Button week1 = (Button) findViewById(R.id.week1button);
Button week2 = (Button) findViewById(R.id.week2button);
Button homework = (Button) findViewById (R.id.homeworkbutton);
//Button Sound
final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);


week1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.WEEK1"));
}});

week2.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.WEEK2"));
}
});
homework.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.HOMEWORK"));
}});};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}




private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
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 picturePath = cursor.getString(columnIndex);
cursor.close();

ImageView imageView = (ImageView) findViewById(R.id.picture);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("picture", picturePath);
edit.commit();


}}}

CODE FOR ADDING PROFILE PIC IS AT TOP AND BOTTOM OF CODE.

HOPE SOMEONE CAN HELP.
MANY THANKS
 
Hi Lewis.
Please can you post a section of the logcat where it falls over?
Thanks.
Thanks for the reply but had a good look at the log cat and online and worked it out that because my phones has 4.4 I needed to give the app permission to use the storage in the android manifest.
However now it loads some photos but some are too BIG, how can I fix this?
 
Best to use a routine to shrink down images and display them on the size of the device. This can drastically save memory. I use such a routine in my apps. Also, don't forget to recycle the space after the image has been used. This is quite important otherwise you will encounter memory leaks.
I us such routines in my Story of Red app, I will try and dig out the code and paste it here before I go to bed.
 
Try something like this:

ImageView viewBigPhoto;
viewBigPhoto = (ImageView) viewYourView.findViewById(R.id.imageBigPhotoImageViewInXML);
viewBigPhoto.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.acttualBigPhotoImage, screenSizeHeight, screenSizeWidth));

to decode image into smaller screen. Also look into Display Metrics to grab the size of the device's screen at runtime.

Hope this helps. :)
 
Try something like this:

ImageView viewBigPhoto;
viewBigPhoto = (ImageView) viewYourView.findViewById(R.id.imageBigPhotoImageViewInXML);
viewBigPhoto.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.acttualBigPhotoImage, screenSizeHeight, screenSizeWidth));

to decode image into smaller screen. Also look into Display Metrics to grab the size of the device's screen at runtime.

Hope this helps. :)

OK thanks very much. I'll try it in a but when I have the time but I appreciate what you've done. Does that code include deleting the picture after choosing a different profile picture like you said or is there no need? Thanks I reasonably new to this and quite young.
Kind regards
 
No, just shrinks it down. You have to modify your code and the example to knit seamlessly together. Thanks
 
I have probably done it wrong but i had to chanage it to this:

ImageView viewBigPhoto;

viewBigPhoto = (ImageView) findViewById(R.id.Profile_picture);
viewBigPhoto.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.avatar1, 225, 225));

The part where is saids r.drawable is where im very confused because the actual picture i want it the one selected by user but when nothing is selected avatar1 is there so iput that at the moment, could you alter your code to fit with my OP code. Hope you dont mind.
Many thanks.
 
Hi Lewis.
I'm at work at the moment so I can't really do that using my phone.
Also, it would be no benefit to you if I was to code your app for you, you wouldn't learn anything.
The best you can expect when you post on a forum, especially on a code developer forum, is to be pointed in the right direction by us more experienced developers.
I'm sorry this might sound a bit short and unhelpful to you, but we've all been in the same position at some time during our coding career.
 
Hello again i know you said you wouldnt help any more but it just still wont work. I am lost and dont know how to fix this problem:


04-02 19:42:22.052: W/OpenGLRenderer(21906): Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096)
 
I'd like it so that any picture the user choses from his gallery (btw this pic is a pic from note 3) and it works in the app. How can I use code to shine it or edit the above given code?
Cheers
 
I appriciate what your doing but i cant seem to make it work :( Im very new to coding and still cant work out why and its frustrating having a little problem that will ruin your whole app. I hope you understand and i hope you can possibly help me more.
Cheers lewis

Here is my code again for mian.java:

Code:
package com.example.timetable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

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

        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String picturePath = prefs.getString("picture", "");
        if(!picturePath.equals("true")){
              ImageView imageView = (ImageView) findViewById(R.id.Profile_picture);
              imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
{
}

    
        Button buttonLoadImage = (Button) findViewById(R.id.picturebutton);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
                
            }{
            
        {
        }
        }});;


            Button week1 = (Button) findViewById(R.id.week1button);
            Button week2 = (Button) findViewById(R.id.week2button);
            Button homework = (Button) findViewById (R.id.homeworkbutton);
        //Button Sound
        final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);


        week1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.timetable.WEEK1"));
            }});

                week2.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        buttonSound.start();
                        startActivity(new Intent("com.example.timetable.WEEK2"));
                    }
        }); 
                homework.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        buttonSound.start();
                        startActivity(new Intent("com.example.timetable.HOMEWORK"));
                    }});};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




private static int RESULT_LOAD_IMAGE = 1;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        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 picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.Profile_picture);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    
            
            SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
	          Editor edit=shre.edit();
	          edit.putString("picture", picturePath);
	          edit.commit();
	          

            }
   
        }}
 
Back
Top Bottom