0

在我的应用程序中有一个按钮可以从图库中选择图像并将图像设置为视图。但这里的问题是,当我单击按钮时,它需要我多次进入画廊。就像我从图库中选择图像一样,它再次要求我从图库中选择图像。这种情况发生 4 - 5 次,然后图像设置为我的自定义视图。每次都是同样的问题

我从图库中选择图像的代码如下,使用视图中的 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);
}

那么我们得到的结果如下

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

我在 ontouch 方法中调用 customview 类中的 tapped 方法。

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

请建议我在这里做错了什么。谢谢你

4

2 回答 2

1

我用它来永远摆脱这个问题

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 0 );
于 2013-10-10T09:24:53.810 回答
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;
 }

供您参考。希望这会对您有所帮助。

从图库中选择多个图像。

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

请参阅参考资料参考资料1

于 2013-08-07T04:48:31.093 回答