本文介绍了Android的:不能调用的onActivityResult()方法,通过摄像头捕获的照片后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序时,我调用由相机意图:

 意图cameraIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_REQUEST);

这不叫的onActivityResult()方法。

问题在我的应用程序是指一段时间内调用此方法,但拍摄的照片经过一段时间后再次来到照片拍摄画面。

之后,我收集按 onRestoreInstanceState(此数据采集照片我节省了大量的数据在的onSaveInstanceState())之前

在这里,我不知道为什么有些时候的onActivityResult()方法调用但过一段时间这种方法进入照片拍摄模式后不会调用。

的onActivityResult()code:

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
    字符串文件名= NULL;
    尝试{
        如果(要求code == CAMERA_REQUEST){
            。roughBitmap =(位图)data.getExtras()获得(数据);
            最后ContentResolver的CR = getContentResolver();
            最终的String [] P1 =新的String [] {
                MediaStore.Images.ImageColumns._ID,
                    MediaStore.Images.ImageColumns.DATE_TAKEN};
            游标c1 = cr.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,P1,空,
                        空,P1 [1] +DESC);
                如果(c1.moveToFirst()){
                    字符串uristringpic =内容://媒体/外部/图像/媒体/
                            + c1.getInt(0);
                    newuri = Uri.parse(uristringpic);
                    // Log.i(TAG,newuri+ newuri);
                    snapName = getRealPathFromURI(newuri);
                    乌里U = Uri.parse(snapName);                    文件f =新的文件(+ U);
                    文件名= f.getName();
                }
                c1.close();                  setImageParameter();            }
        }赶上(NullPointerException异常五){        }
        的System.out.println(***的onActivityResult()结束***);
    }公共无效setImageParameter(){
    //德code完整图像
    roughBitmap = BitmapFactory.de codeFILE(snapName);
    //计算确切的目标大小
    字模=新的Matrix();
    RectF inRect =新RectF(0,0,roughBitmap.getWidth(),
            roughBitmap.getHeight());
    // RectF outRect =新RectF(0,0,dstWidth,dstHeight);
    RectF outRect =新RectF(0,0,640,480);
    matrix.setRectToRect(inRect,outRect,Matrix.ScaleToFit.CENTER);
    浮动[] =值新的浮动[9];
    matrix.getValues​​(值);
    //调整位图
    resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
            (中间体)(roughBitmap.getWidth()*值[0]),
            (中间体)(roughBitmap.getHeight()*值[4]),真);    油漆=新的油漆();
    paint.setColor(Color.GREEN);
    paint.setTextSize(16);
    paint.setTextAlign(Paint.Align.LEFT);    帆布帆布=新的Canvas(resizedBitmap);    canvas.drawText(将String.valueOf(LAT),resizedBitmap.getWidth() - 290,resizedBitmap.getHeight() - 50,油漆);
    canvas.drawText(将String.valueOf(LNG),resizedBitmap.getWidth() - 140,resizedBitmap.getHeight() - 50,油漆);
    如果(!editTextRoadName.getText()。的toString()。equalsIgnoreCase()){
        canvas.drawText(editTextRoadName.getText()的toString(),resizedBitmap.getWidth() - 290,resizedBitmap.getHeight() - 30,涂料等。);
    }
    canvas.drawText(新DateClass()getSysDateTimeForPhoto(),resizedBitmap.getWidth() - 290,resizedBitmap.getHeight() - 10,涂料等。);    如果(nFinalOrientation == 1){
        matrix.postRotate(90);
    }其他{
        matrix.postRotate(0);
    }
    rotatedBitmap = Bitmap.createScaledBitmap(resizedBitmap,
            (中间体)(resizedBitmap.getWidth()*值[0]),
            (中间体)(resizedBitmap.getHeight()*值[4]),真);    如果(booleanPhotoFlag){
        booleanPhotoFlag = FALSE;
        photoBitmap = rotatedBitmap.copy(Bitmap.Config.ARGB_8888,真);
        imageViewPhoto.setImageBitmap(photoBitmap);    }其他{
        landmarkBitmap = rotatedBitmap.copy(rotatedBitmap.getConfig(),rotatedBitmap.isMutable()真的?假的);
        imageViewLocationPhoto.setImageBitmap(landmarkBitmap);
    }}


解决方案

我创建了一个,你可以用它来拍照。我建议你​​使用这个库进行拍照

编辑2:

看试着将之间的日志保护无效的onActivityResult(INT申请code,INT结果code,意图数据) super.onActivityResult(要求code,结果code,数据); 来看看如果连这个方法被调用或没有,如果它被称为然后检查返回的值。 总的onActivityResult不是由您的活动但在这种情况下,调用活动意图 MediaStore.ACTION_IM​​AGE_CAPTURE 让你的onActivityResult必须由的setResult()调用。但我希望你不是要通过传递意图喜欢这里

In my application when I call camera intent by:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

it not call onActivityResult() method.

Problem in my application is that some time it calls this method, but some time after capturing photo it again come to photo capture screen.

Before capturing photo I am saving a lot of data in onSaveInstanceState() after that I am collecting this data by onRestoreInstanceState().

Here I don't know why some time onActivityResult() method calls but some time this method does not call after entering into the photo capture mode.

onActivityResult() code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    String fileName = null;
    try {
        if (requestCode == CAMERA_REQUEST) {
            roughBitmap = (Bitmap) data.getExtras().get("data");
            final ContentResolver cr = getContentResolver();
            final String[] p1 = new String[] {
                MediaStore.Images.ImageColumns._ID,
                    MediaStore.Images.ImageColumns.DATE_TAKEN };
            Cursor c1 = cr.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null,
                        null, p1[1] + " DESC");
                if (c1.moveToFirst()) {
                    String uristringpic = "content://media/external/images/media/"
                            + c1.getInt(0);
                    newuri = Uri.parse(uristringpic);
                    // Log.i("TAG", "newuri "+newuri);
                    snapName = getRealPathFromURI(newuri);
                    Uri u = Uri.parse(snapName);

                    File f = new File("" + u);
                    fileName = f.getName();
                }
                c1.close();

                  setImageParameter();

            }
        } catch (NullPointerException e) {

        }
        System.out.println("*** End of onActivityResult() ***");
    }

public void setImageParameter() {
    // decode full image
    roughBitmap = BitmapFactory.decodeFile(snapName);
    // calc exact destination size
    Matrix matrix = new Matrix();
    RectF inRect = new RectF(0, 0, roughBitmap.getWidth(),
            roughBitmap.getHeight());
    // RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
    RectF outRect = new RectF(0, 0, 640, 480);
    matrix.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
    float[] values = new float[9];
    matrix.getValues(values);
    // resize bitmap
    resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
            (int) (roughBitmap.getWidth() * values[0]),
            (int) (roughBitmap.getHeight() * values[4]), true);

    paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setTextSize(16);
    paint.setTextAlign(Paint.Align.LEFT);

    Canvas canvas = new Canvas(resizedBitmap);

    canvas.drawText(String.valueOf(lat), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 50, paint);
    canvas.drawText(String.valueOf(lng), resizedBitmap.getWidth() - 140, resizedBitmap.getHeight() - 50, paint);
    if (!editTextRoadName.getText().toString().equalsIgnoreCase("")) {
        canvas.drawText(editTextRoadName.getText().toString(), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 30, paint);
    }
    canvas.drawText(new DateClass().getSysDateTimeForPhoto(), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 10, paint);



    if (nFinalOrientation == 1) {
        matrix.postRotate(90);
    } else {
        matrix.postRotate(0);
    }


    rotatedBitmap = Bitmap.createScaledBitmap(resizedBitmap,
            (int) (resizedBitmap.getWidth() * values[0]),
            (int) (resizedBitmap.getHeight() * values[4]), true);

    if (booleanPhotoFlag) {
        booleanPhotoFlag = false;
        photoBitmap = rotatedBitmap.copy(Bitmap.Config.ARGB_8888, true);
        imageViewPhoto.setImageBitmap(photoBitmap);

    } else {
        landmarkBitmap = rotatedBitmap.copy(rotatedBitmap.getConfig(), rotatedBitmap.isMutable() ? true : false);
        imageViewLocationPhoto.setImageBitmap(landmarkBitmap);
    }

}
解决方案

I have created a Camera Library Project you can use this to take picture. I would suggest you to use this library to take picture

EDIT 2:

Look try placing a log between protected void onActivityResult(int requestCode, int resultCode, Intent data) and super.onActivityResult(requestCode, resultCode, data); to see if even that method is being called or not, If its called then check the value that is returned. AND The onActivityResult is not called by your activity but by the calling activity in this case the intent of MediaStore.ACTION_IMAGE_CAPTURE so your onActivityResult has to be called by the setResult() of the calling activity. But I hope your not trying to pass the bitmap via intent like here

这篇关于Android的:不能调用的onActivityResult()方法,通过摄像头捕获的照片后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:30