0

can anyone help me to solve this problem, how to call image from gallery/imagebutton to canvas for drawing? this is my snippet code :

buah1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
             startActivity(intent); 
        }   
    });

If anyone need a project I will send my project to email.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
  • http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/ – K Neeraj Lal May 28 '15 at 13:20
  • http://stackoverflow.com/questions/5890809/how-to-set-an-image-to-fill-a-canvas – K Neeraj Lal May 28 '15 at 13:21
  • 1
    U question is not understandable, do u want to pick an image from image gallery on a button click??? – DJphy May 28 '15 at 13:21
  • yes, i want pick a image from gallery/imagebutton to drawingview in another layout @DJphy – Luthfi Atma Nugroho May 28 '15 at 13:26
  • 1
    so you want to pick an image from sd card, or internal storage and load it in your imageView or canvas?? – Hawraa Khalil May 28 '15 at 13:35
  • on How to transfer images from one activity to another see this link http://stackoverflow.com/questions/30407369/base64-image-conversion-in-android-resulting-in-corrupted-image/30432397#30432397 – DJphy May 28 '15 at 13:49
  • not like that sir @HawraaKhalil,i have some picture using imagebutton to showing and i mean is how to show picture from imagebutton to other layout for drawable. i cant speak spesific because i cant upload screenshot from my project, example (my screenshot) : [IMG]http://i1296.photobucket.com/albums/ag13/Luthfi_Atma_Nugroho/Call%20image_zpsq2qrfd1s.jpg[/IMG][/URL] – Luthfi Atma Nugroho May 28 '15 at 13:55
  • okay, i got your point, you want to load(open) one of those images in the canvas in another activity, right? are those images already found in the application, in drawable or something? – Hawraa Khalil May 28 '15 at 14:03
  • yes sir @HawraaKhalil, that images already stored in drawable folder – Luthfi Atma Nugroho May 28 '15 at 14:17

2 Answers2

2

Actually i wanted to post the link in the comment for u but u seemed to bit less fimiliar with android(i thinking so), So i am posting my code to do that. First choose on which button click u want the user to be guided to gallery(only default gallery and not anything else, u may get a null pointer if u choose pic from any other).

On that button click u do this:

private void onClickOfButton(View v){

    Intent galleryIntent=new Intent();
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(Intent.createChooser(galleryIntent, "pic-select"), 10);//(10 its just a request code, u can give ur own, but same should there at receiving end )
}

Since u have started an activity for result, so u should be awaiting for a result from that started activity, so that activity will bring u the image and in ur activity u just collect it and put it into ur image view like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode==10 && resultCode==Activity.RESULT_OK){
            try{

                Uri selectImageUri=data.getData();
                String selectedImagePath=getPath(selectImageUri);
                Bitmap pic=BitmapFactory.decodeFile(selectedImagePath);
                if(pic!=null){
                    yourImageView.setImageBitmap(pic);

                }

            }catch(NullPointerException ex){
                Toast.makeText(getActivity().getBaseContext(), "Go to default gallery area and choose a pic", Toast.LENGTH_LONG).show();
            }

        }
        else
            super.onActivityResult(requestCode, resultCode, data);

}

The getPath method:

private String getPath(Uri uri) {
    if( uri == null ) {
        return null;
    }
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
}

So this will do ur job...

DJphy
  • 1,292
  • 1
  • 17
  • 31
0

try this:

 buah1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(),     BelajarMewarnai.class);
            Bundle bundle = new Bundle();
//Get the image name you clicked on
            String imageName =     getResources().getResourceName(imageId);;
//Send the image name as a string, which you want      to load in the other activity
            bundle.putString("image_to_load", imageName.split("/")[1].toString());
            intent.putExtras(bundle);
            startActivity(intent); 
        }   
    });

then you can get your image name in the next activity in onCreate like this:

 //Here you can get the image name you already  clicked in the previous activity to load in your canvas
        Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                String image_name = bundle.getString("image_to_load")
        }

then you can set the image name to your imageView like this:

 //Here you can get all resources in res folder
     Resources res = getResources();
    //define a new string which takes the image name you get from extras in the above code
       String mDrawableName = image_name;
    //Now you have the image name you clicked in theprevious activity, and ResID is to get the resource id     from its name
        int resID = res.getIdentifier(mDrawableName, "drawable", getPackageName());
    //Here you can get the drawable from the   resourceID you obtained in the above line
       Drawable drawable = res.getDrawable(resID );
    YOUR_IMAGE is the ImageView of the canvas you   want to load the image inside in order to draw or    whatever you want to do
      drawView.setBackgroundDrawable(drawable);

I hope this will help..

Hawraa Khalil
  • 261
  • 2
  • 12
  • have one account, example facebook,gmail or other. i have one question sir. @Hawraa Khalil – Luthfi Atma Nugroho May 28 '15 at 15:33
  • can you explain about Resources res = getResources(); String mDrawableName = image_name; int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName()); Drawable drawable = res.getDrawable(resID ); YOUR_IMAGE.setImageDrawable(drawable); cause i don't know what the mean of YOUR_IMAGE bla bla bla – Luthfi Atma Nugroho May 28 '15 at 17:09
  • I will edit my code and add comments to explain the above, and yes I have Facebook, just search for my name and u will get a picture for chess, its me – Hawraa Khalil May 28 '15 at 17:25
  • Please give me access to the project link to help you more, and update my answer to fit your question @LuthfiAtmaNugroho – Hawraa Khalil May 29 '15 at 05:59