2013-08-07 6 views
0

Dans mon application, il y a un bouton pour sélectionner l'image de la galerie et définir l'image à la vue. Mais le problème ici est quand je clique sur le bouton, il me prend à la galerie plusieurs fois. Comme si je sélectionnais une image de la galerie, il me demandait à nouveau de choisir l'image de la galerie. Cela se produit 4 à 5 fois, puis l'image est définie sur ma vue personnalisée. Chaque fois c'est le même problèmeandroid startActivityForResult ayant des problèmes

Mon code pour sélectionner l'image de la galerie est comme suit en utilisant startactivityforresult qui est en doebletap sur la vue. Quand je tape deux fois sur la vue, la galerie s'ouvre pour choisir l'image. c'est pour plusieurs vues. chaque vue doit être définie avec une image différente de la galerie. donc j'utilise cette méthode.

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

alors le résultat que nous obtenons est la suivante

@Override onActivityResult public void (int requestCode, int resultCode, les données d'intention) {

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

J'appelle la méthode taraudé la classe customview dans la méthode ontouch.

Ma méthode est OnTouch comme suit

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

S'il vous plaît me suggérer ce que je fais mal ici. merci

Répondre

1

J'ai utilisé ce pour se débarrasser de ce problème pour toujours

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

utilisation comme celui-ci

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

Pour votre reference .Hope cela vous aidera.

Sélection de plusieurs images dans la galerie.

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

Voir la reference, reference1

+0

No. toujours le même problème. Je suis vraiment frustré. En fait, mon application contient plusieurs vues de sorte que l'activité de démarrage multiple pour résultat. Aidez-moi, s'il vous plaît. –

+0

alors le problème n'est pas dans l'activité de démarrage pour result.post votre code complet. – Nirmal

+0

a édité ma question s'il vous plaît vérifier. –

Questions connexes