0

Hello I have a timetable app which has a main menu. I would like to have a button that saids 'choose image' or something like that so when a user clicks on it he enters his gallery and selects photo and then it saves on the timetable app I've created. I hope someone can help me as the only info I've found on the Internet is that I should use android.intent.action.PICK? Thanks in advance

UPDATE: I did this and then now when I click the button to load gallery (which works perfectly). Once I choose an image, it says "Unfortunately, timetable has stopped". What's wrong. Here's the code.

import android.media.MediaPlayer;
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.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);


        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        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 Sound
        final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);

        //Setting up the button references
        Button week1 = (Button) findViewById(R.id.week1button);
        Button week2 = (Button) findViewById(R.id.week2button);

        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"));
                    }
        });       

          };

    @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.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}
Java_User
  • 1,303
  • 3
  • 27
  • 38
user3195144
  • 23
  • 1
  • 6
  • Google "android.intent.action.PICK" search what is its uses and if it's OK for you. No? Search "pick image intent android" and you get this http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app-in-android Maybe this could help you. – Marco Acierno Feb 11 '14 at 20:19
  • CAN YOU HAVE A LOOK AT THE ORIGINAL POST AS I'VE CHANGED IT. IT DOESNT SEEM TO WORK PROPERLY?!? – user3195144 Feb 12 '14 at 18:14

1 Answers1

0

Assuming it's a social application then I'd suggest using this tutorial or this SO question to import the picture and then save it in a remote database.

Community
  • 1
  • 1
Random
  • 431
  • 8
  • 20
  • Ok thanks for the links. I couldn't find much myself. I've had a quick look and looks like what I need so will try tomorrow and I'll let you know if it works. Thanks – user3195144 Feb 11 '14 at 20:43
  • CAN YOU HAVE A LOOK AT THE ORIGINAL POST AS I'VE CHANGED IT. IT DOESNT SEEM TO WORK PROPERLY?!? – user3195144 Feb 12 '14 at 18:12