Question

In my app there is a button to select image from gallery and set the image to the view. But the problem here is when i click the button, it takes me to gallery multiple times. Like if I select an image from gallery , it again ask me to chose image from gallery. This happens 4 - 5 times and then the image is set to my customview. Everytime its the same problem

My code to select image from gallery is as follows using startactivityforresult which is in doebletap on the view.When i double tap on the view ,the gallery opens up to choose picture. this is for multiple views. each view should be set with different picture from gallery. so i use this methods.

 static void tapped1(Context context, int requestCode){

((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
}

static void tapped2(Context context, int requestCode){

((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 2);
}

static void tapped3(Context context, int requestCode){

((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 3);
}

static void tapped4(Context context, int requestCode){

((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 4);
}

static void tapped5(Context context, int requestCode){

((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 5);
}

then the result we get is as follows

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

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK) {

    if(requestCode==1){

        reset();

    imageURI = data.getData(); 
    try {
        Bitmap b = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI);

         PanView.imageBitmap=b; 

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }

    PanView.invalidate(); 
    }

    if(requestCode==2){

        reset();

        imageURI = data.getData(); 
        try {
            Bitmap b = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI);

             PanView1.imageBitmap=b; 


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }

        PanView1.invalidate(); 

        }

    if(requestCode==3){

        reset();

        imageURI = data.getData(); 
        try {
            Bitmap b = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI);

             PanView2.imageBitmap=b; 

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }

        PanView2.invalidate(); 

        }

    if(requestCode==4){

        reset();

        imageURI = data.getData(); 
        try { 
            Bitmap b = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI);

             PanView3.imageBitmap=b; 

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }

        PanView3.invalidate(); 

        }   

if(requestCode==5){

reset();

        imageURI = data.getData(); 
        try { 
            Bitmap b = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI);

             PanView4.imageBitmap=b; 

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }

        PanView4.invalidate(); 

        }


} else {
    System.exit(0);
    Log.e("result", "BAD");
}
}

I am calling the tapped method in the customview class in ontouch method.

My ontouch method is as follows

public boolean onTouchEvent(MotionEvent ev) {

// If we are not supporting either zoom or pan, return early.
if (!mSupportsZoom && !mSupportsPan) return false;

// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);

final int action = ev.getAction();

if ( Math.abs(mDeBounce - ev.getEventTime()) < 150) {
    //Ignore if it's been less then 250ms since
  //the item was last clicked
    ((PhotoCollageActivity)  mContext).tapped1(this.getContext(), 1);

    return true;
}

int intCurrentY = Math.round(ev.getY());
int intCurrentX = Math.round(ev.getX());
int intStartY = ev.getHistorySize() > 0 ? Math.round(ev.getHistoricalY(0)) : intCurrentY;
int intStartX = ev.getHistorySize() > 0 ? Math.round(ev.getHistoricalX(0)) : intCurrentX;



if ( (ev.getAction() == MotionEvent.ACTION_UP) && (Math.abs(intCurrentX - intStartX) < 3) && (Math.abs(intCurrentY - intStartY) < 3) ) {
//        if ( mDeBounce > ev.getDownTime() ) {
        //Still got occasional duplicates without this

    //Handle the click


    mDeBounce = ev.getEventTime();
    return true;

}

switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
     final int CONST = 5;
    final float x = ev.getX();
    final float y = ev.getY();

    mLastTouchX = x;
    mLastTouchY = y;

    mLastTouchXMax = x+CONST;  //here i get x and y values in action down
    mLastTouchXMin = x-CONST;
    mLastTouchYMax = y+CONST;
    mLastTouchYMin = y-CONST;
    mActivePointerId = ev.getPointerId(0);

    break;
}

case MotionEvent.ACTION_MOVE: {
    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
    final float x = ev.getX(pointerIndex);
    final float y = ev.getY(pointerIndex);

    // Only move if the view supports panning and
    // ScaleGestureDetector isn't processing a gesture.
    if (mSupportsPan && !mScaleDetector.isInProgress()) {
        final float dx = x - mLastTouchX;
        final float dy = y - mLastTouchY;

        mPosX += dx;
        mPosY += dy;
        //mFocusX = mPosX;
        //mFocusY = mPosY;

        invalidate();
    }

    mLastTouchX = x;
    mLastTouchY = y;

    break;
}

case MotionEvent.ACTION_UP: {

    final float x = ev.getX();
    final float y = ev.getY();

    touchupX=x;
    touchupY=y;




    mActivePointerId = INVALID_POINTER_ID;

    break;
}

case MotionEvent.ACTION_CANCEL: {
    mActivePointerId = INVALID_POINTER_ID;
    break;
}

case MotionEvent.ACTION_POINTER_UP: {
    final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
            >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastTouchX = ev.getX(newPointerIndex);
        mLastTouchY = ev.getY(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }
    break;
}
}


return true;
}

Please suggest me what i am doing wrong here. thank you

Was it helpful?

Solution

I used this to get rid of that problem forever

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 0 );

OTHER TIPS

Use like this

                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                 intent.setType("image/*");
                 startActivityForResult(intent, 0);


protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
      super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

      switch(requestCode) {
      case 0:
          if(resultCode == RESULT_OK){
              Uri selectedImage = imageReturnedIntent.getData();

              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

              cursor.moveToFirst();

              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

             //file path of selected image
              filePath = cursor.getString(columnIndex);
              File f = new File(filePath);
              filename= f.getName();

              Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show();
              Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show();
              cursor.close();


          }
          break;
 }

For your reference.hope this will help you.

Selecting multiple images from gallery.

Intent intent = new Intent();
intent.setType('image/*');
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, 'Select Picture'), PICK_IMAGE);

See the reference,reference1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top