Camera Intent仅在某些设备上返回null [英] Camera Intent return null only on some devices

查看:105
本文介绍了Camera Intent仅在某些设备上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用相机捕获图像并将返回的位图设置为imageview,但是在OnePlus设备上崩溃.我检查了几台htc设备,它运行正常.我检查了解决方案,他们说要检查请求代码,但我已经在做了.这可能是什么问题?

I am using camera to capture image and set the returned bitmap into an imageview but it is crashing on OnePlus devices. I checked on few htc devices and it is working perfectly. I checked for solutions and they said to check for request code but I am already doing it. What can be the problem here?

这是代码

 imageView.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(cameraIntent, 1);
        }
    });

和onActivityResult

and onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == -1) {
        image = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        // Must compress the Image to reduce image size to make upload easy
        image.compress(Bitmap.CompressFormat.PNG, 50, stream);
        byte[] byte_arr = stream.toByteArray();
        // Encode Image to String
        encodedString = Base64.encodeToString(byte_arr, 0);
        Uri selectedImageUri = data.getData();
        img_path = getRealPathFromURI(selectedImageUri);
      //  Uri tempUri = getImageUri(getApplicationContext(), image);
        imageView.setImageBitmap(image);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }
}

和错误的堆栈跟踪

STACK_TRACE = java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 1,result = -1,data = Intent { act = inline-data(有附加内容)}}进行活动 {com.madhours/com.madhours.activities.ActivitySignUp}: java.lang.NullPointerException:尝试调用虚拟方法 空对象上的'java.lang.String android.net.Uri.getPath()' 参考于 android.app.ActivityThread.deliverResults(ActivityThread.java:3659) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:155) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1366) 在android.os.Handler.dispatchMessage(Handler.java:102)处 android.os.Looper.loop(Looper.java:135)在 android.app.ActivityThread.main(ActivityThread.java:5343)位于 java.lang.reflect.Method.invoke(本机方法)位于 java.lang.reflect.Method.invoke(Method.java:372)在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 原因:java.lang.NullPointerException:尝试调用虚拟 空对象上的方法'java.lang.String android.net.Uri.getPath()' 参考于 com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) 在 com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) 在android.app.Activity.dispatchActivityResult(Activity.java:6218) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3655) ... 10更多java.lang.NullPointerException:尝试调用虚拟 空对象上的方法'java.lang.String android.net.Uri.getPath()' 参考于 com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) 在 com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) 在android.app.Activity.dispatchActivityResult(Activity.java:6218) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3655) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:155) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1366) 在android.os.Handler.dispatchMessage(Handler.java:102)处 android.os.Looper.loop(Looper.java:135)在 android.app.ActivityThread.main(ActivityThread.java:5343)位于 java.lang.reflect.Method.invoke(本机方法)位于 java.lang.reflect.Method.invoke(Method.java:372)在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

STACK_TRACE=java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.madhours/com.madhours.activities.ActivitySignUp}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3659) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) at com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) at android.app.Activity.dispatchActivityResult(Activity.java:6218) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) ... 10 more java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) at com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) at android.app.Activity.dispatchActivityResult(Activity.java:6218) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

推荐答案

这里可能是什么问题?

What can be the problem here?

问题是您正在返回的Intent上调用getData().返回Uri不属于 ACTION_MEDIA_STORE合同:

The problem is that you are calling getData() on the returned Intent. Returning a Uri is not part of the ACTION_MEDIA_STORE contract:

调用方可以传递一个额外的EXTRA_OUTPUT来控制将图像写入何处.如果不存在EXTRA_OUTPUT,则会在Extra字段中将一个小型图像作为Bitmap对象返回.这对于只需要较小图像的应用程序很有用.如果存在EXTRA_OUTPUT,则将完整尺寸的图像写入EXTRA_OUTPUT的Uri值.

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

鉴于您没有将EXTRA_OUTPUT放在与startActivityForResult()一起使用的Intent上,所以唯一得到的结果是data附加项中的Bitmap.

Given that you did not put EXTRA_OUTPUT on the Intent you used with startActivityForResult(), the only result you will get is the Bitmap in the data extra.

如果要在某处写入完整尺寸的图像,请在EXTRA_OUTPUT中提供指向该位置的路径,然后保留该路径并在调用onActivityResult()时使用它:

If you want a full-size image written somewhere, provide a path to that location in EXTRA_OUTPUT, then hold onto that path and use it when onActivityResult() is called:

/***
 Copyright (c) 2008-2016 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final String EXTRA_FILENAME=
    "com.commonsware.android.camcon.EXTRA_FILENAME";
  private static final String FILENAME="CameraContentDemo.jpeg";
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (savedInstanceState==null) {
      File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

      dir.mkdirs();
      output=new File(dir, FILENAME);
    }
    else {
      output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    if (output.exists()) {
      output.delete();
    }

    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

这篇关于Camera Intent仅在某些设备上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆