【问题标题】:android startActivityForResult having problemsandroid startActivityForResult 有问题
【发布时间】:2013-08-07 04:45:00
【问题描述】:

在我的应用程序中有一个按钮可以从图库中选择图像并将图像设置为视图。 但这里的问题是,当我单击按钮时,我需要多次访问画廊。就像我从图库中选择图像一样,它再次要求我从图库中选择图像。这种情况发生 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);
}

那么我们得到的结果如下

@覆盖 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;
}

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

【问题讨论】:

    标签: android view gallery photo-gallery start-activity


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这样使用

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

      reference,reference1

      【讨论】:

      • 没有。还是一样的问题。我真的很沮丧。实际上我的应用程序包含多个视图,因此多个启动活动的结果。请帮帮我。
      • 那么问题不在于结果的开始活动。发布您的完整代码。
      • 你为什么要用5个startActivityForResult所以给你添麻烦了。
      • 我使用五个开始活动来获得结果,因为我有 5 个视图,每个视图都需要不同的图片。有没有其他方法可以用单个 startactivityfor 结果来做到这一点?
      • 5 个包含位图的自定义视图。我将从图库中选择的图像加载到每个自定义视图中的位图。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多