2013-08-07 59 views
0

在我的應用程序中有一個按鈕,從圖庫中選擇圖像並將圖像設置爲視圖。 但這裏的問題是當我點擊按鈕,它需要我多次畫廊。就像如果我從圖庫中選擇圖像一樣,它會再次要求我從圖庫中選擇圖像。這發生了4 - 5次,然後圖像被設置爲我的customview。每當它出現同樣的問題android startActivityForResult有問題

我從代碼庫中選擇圖片的代碼如下,使用在視圖中的doebletap中的startactivityforresult。當我雙擊視圖時,畫廊打開選擇圖片。這是用於多個視圖。每個視圖都應該設置與畫廊不同的圖片。所以我使用這種方法。

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

那麼我們得到的結果如下

@覆蓋 公共無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據){

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

我打電話的挖掘方法ontouch方法中的customview類。

我ontouch方法如下

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

請建議我我在做什麼錯在這裏。謝謝

回答

1

我用這個來擺脫這個問題永遠

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

使用這樣

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

爲了您reference。希望這會幫助你。

從圖庫中選擇多個圖像。

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

referencereference1

+0

號還是同樣​​的問題。我真的很沮喪。實際上我的應用程序包含多個視圖,因此多個開始活動的結果請幫幫我。 –

+0

那麼問題不在result.post你的完整代碼的開始活動。 – Nirmal

+0

編輯我的問題,請檢查。 –