1

I am new to android development, and I am creating my first android application, Little bit about the app, It is Drawing App, but I have also added outlined images to the app loading the images into a GridView, I want the user to select the image so that the image comes back to the drawing view activity and user can paint over it. below Is my code that I am using... I am stuck, as when I select image from the grid view the selected image does not appear onto the drawing view. How to do that ?

This is MainActivity.class where the drawing view and behind it imageview is placed

`Intent imagePickIntent = new Intent(MainActivity.this, GalleryView.class);
        imagePickIntent.setType("image/*");
        imagePickIntent.setAction(Intent.ACTION_PICK);
        startActivityForResult(imagePickIntent, SELECT_PHOTO);`

This is GalleryView which has grid view in it where array of images are stored

final GridView gridview = (GridView) findViewById(R.id.selectGridView);
    gridview.setAdapter(new SelectImageAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Intent intent = new Intent(GalleryView.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(GalleryView.this, "" + position,
                    Toast.LENGTH_SHORT).show();

Please help in selecting the image from the grid view, that after user selects the image the image should appear in the Main Activity to colour on to.

Thank you

sulli110
  • 145
  • 2
  • 16
  • Questions about Android programming could possibly go on android.stackexchange.com but I do not see the option to flag it as off-topic and belonging on that site. – Andrew Lau Mar 14 '18 at 15:01
  • Thank you for the feed back i will change the category.. – sulli110 Mar 15 '18 at 03:16

1 Answers1

0

This is sample code to use. I have implemented the below code in an app and it works. I added comments that you may follow along. Hope it helps.

// at the start of main activity class //have a static for the request code 


public class MainActivity extends Activity implements OnClickListener {

    private static final int FILE_DIALOGUE_ACTIVITY_REQUEST_CODE = 1;


//In activity Main Activity fire the intent to get result



Intent intentFileDialoge = new Intent (MainActivity.this, AndroidExplorer.class);
//get the file opened in the start of the activity get the resulting code in the static 
//FILE_DIALOGUE_ACTIVITY_REQUEST_CODE
startActivityForResult(intentFileDialoge, FILE_DIALOGUE_ACTIVITY_REQUEST_CODE);




    // on activity a override on activity result to get the result from the activity using the string result 
    //that is added from the activity with the string "result"
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check the result code is 1 
        if (requestCode == FILE_DIALOGUE_ACTIVITY_REQUEST_CODE) {
            if(resultCode == Activity.RESULT_OK){

                //get the string from the "result"
                String  resultFromFileDialogueActivity=data.getStringExtra("result");

                //use the result to do with it what you want

                // this is an example of getting the result here being resultFromFileDialogueActivity 
                // and using it in a third activity . Activity c here is being started using the data 
                //from activity B that has 
                //been returned
                    Intent intentOpenFileInEditor = (new Intent (MainActivity.this, NotePadEditor.class));
                    intentOpenFileInEditor.putExtra("fileToOpen", resultFromFileDialogueActivity);
                    startActivity(intentOpenFileInEditor);

            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }




//in the activity B where you are getting the result from 
// eg once an  onclick 
// listener  has finished and you have your file from the grid view
//


    //get the calling intent
    Intent returnIntent = getIntent();  
    //return the name of the file once you have finished picking the file
    //note file here is a varible that i already declared elsewhere and it contails the
    // file i need 
    returnIntent.putExtra("result", file.getPath());
    //set result ok if activity has finished without error
    setResult(Activity.RESULT_OK,returnIntent);
// finish the activity
    finish();
arthur kay
  • 116
  • 7
  • Comment anything you don't understand . I'll try to explain as i understood it. – arthur kay Mar 15 '18 at 12:30
  • @ Arthur Kamande Thanks a lot for explaining and answering to my problem, I understood most of the part and I tried it with my app, just testing it with making a Toast. If you can help me further, I want my image to sent from say activity B which is a grid view to activity A which is a drawing view, using the above code which you helped with, how should I do it ? Thanks again for the help. – sulli110 Mar 15 '18 at 13:42
  • Welcome that was my whole day a few weeks ago doing that . The grid view is it returning a file location? . My code just assumes that . Handle that using the onclicked event of the grid. Maybe google how to get the exact item clicked in gridview then get the file and return the location to your image view on the returnintent extras. – arthur kay Mar 15 '18 at 15:17
  • gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { String a = String.valueOf(position); Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show(); } }); I think thats the appropriate place to put the return intent and finish activity B. – arthur kay Mar 15 '18 at 15:24
  • see this question https://stackoverflow.com/questions/12734793/android-get-position-of-clicked-item-in-gridview – arthur kay Mar 15 '18 at 15:26
  • The gridview is not returning a file location, the grid view contains an array of images, there is select button in the drawing view, which takes the user to grid view activity and when user selects the image from the grid view, the users is taken back to the drawing view so that the user can colour over the image. – sulli110 Mar 15 '18 at 15:58
  • The issue is after clicking the on the image, the users goes back to the drawing view, but the image is not loaded from the grid view to the drawing view. – sulli110 Mar 15 '18 at 15:59
  • Can you get the r. Id of the image clicked. I think on the clicked event handler you can get clicked image by the id – arthur kay Mar 15 '18 at 16:49
  • Post the code for click event for clickhandler for grid – arthur kay Mar 15 '18 at 16:51