【问题标题】:camera captured image intent to other activity?相机捕捉到其他活动的图像意图?
【发布时间】:2016-01-09 21:19:21
【问题描述】:

这是在 StackOverflow 上看到的非常常见的问题,我已经尝试了几乎所有关于 SO 的解决方案。我仍然在onAtivityResult- data 中获得NullPointerExeption

我试过了:

我第四次尝试的最后一个代码如下:

点击按钮:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File file=getOutputMediaFile(1);
                picUri = Uri.fromFile(file); // create
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri); // set the image file
                    startActivityForResult(cameraIntent, REQUEST_TAKE_PHOTO);

getOutputMediaFile()

 private  File getOutputMediaFile(int type) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyApplication");

        /**Create the storage directory if it does not exist*/
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }

        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == 1){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".png");
        } else {
            return null;
        }

        return mediaFile;
    }

onActivityResult()

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO && intent != null){


            Uri selectedImage = intent.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 captured image
            imagepath = cursor.getString(columnIndex);
            //file path of captured image
            File f = new File(imagepath);
            String filename = f.getName();
            cursor.close();
            Bitmap imageData = BitmapFactory.decodeFile(filename);
//            Bitmap imageData = (Bitmap) data.getExtras().get("data");

            Intent i = new Intent(this, EditImageActivity.class);
            i.putExtra("path", imageData );
            startActivity(i);
        }
    }
}

请建议一些更好的东西,这些在我已经提到的链接中没有提到,因为这对我不起作用。

我正在使用带有 api 10+ 的 Android Atudio。

崩溃日志:

10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime: FATAL EXCEPTION: main
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime: Process: com.example.dell.drawdemo, PID: 25060
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.dell.drawdemo/com.example.dell.drawdemo.MainActivity}: java.lang.NullPointerException
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3593)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3636)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.access$1300(ActivityThread.java:151)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5334)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.example.dell.drawdemo.MainActivity.onActivityResult(MainActivity.java:131)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:5546)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3589)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3636) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5334) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

【问题讨论】:

  • 10-12 17:20:27.534 17724-17724/com.example.dell.drawdemo E/AndroidRuntime: java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=1, result=-1, data=null} 到活动 {com.example.dell.drawdemo/com.example.dell.drawdemo.MainActivity}:java.lang.NullPointerException
  • 10-12 17:20:27.534 17724-17724/com.example.dell.drawdemo E/AndroidRuntime: 原因:java.lang.NullPointerException 10-12 17:20:27.534 17724-17724 /com.example.dell.drawdemo E/AndroidRuntime:在 com.example.dell.drawdemo.MainActivity.onActivityResult(MainActivity.java:129)
  • 希望这能有所帮助 - @Jas
  • @DrashtiKapadia 使用 logcat 输出编辑您的问题,在 cmets 中阅读会令人困惑

标签: android android-intent camera


【解决方案1】:

您好,请尝试以下代码,希望对您有所帮助,

private final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private final int MEDIA_TYPE_IMAGE = 1;

捕获图像的意图代码

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
m_fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, m_fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);


public Uri getOutputMediaFileUri(int p_type)
{
    return Uri.fromFile(getOutputMediaFile(p_type));
}


private File getOutputMediaFile(int p_type)
{
    File m_mediaFile;
    if (p_type == MEDIA_TYPE_IMAGE)
    {

            m_buffer = new StringBuffer();

            m_buffer.append(Environment.getExternalStorageDirectory().getPath() + File.separator).append("Image");
            File m_imageStorageFile = new File(m_buffer.toString());

            // Create the storage directory if it does not exist
            if (!m_imageStorageFile.exists())
            {
                if (!m_imageStorageFile.mkdirs())
                {
                    return null;
                }
            }
            // Create a media file name
            String m_timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new java.util.Date());
            m_buffer = new StringBuffer();
            m_buffer.append(m_imageStorageFile.getPath()).append(File.separator).append("IMG_").append(m_timeStamp).append(".jpg");
            m_mediaFile = new File(m_buffer.toString());

    }
    else
    {
        return null;
    }

    return m_mediaFile;
}

在你的 onActivityResult 中写下代码

protected void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data)
{
        super.onActivityResult(p_requestCode, p_resultCode, p_data);
        if (p_resultCode == RESULT_OK)
        {
            switch (p_requestCode)
            {
                case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
                    m_selectedPath = m_fileUri.getPath();
                break;

            }
        }
}

Constant.MEDIA_DIRECTORY_PATH 包含您的目录路径 示例:- mnt/sdcard0/android/com.example

【讨论】:

  • 你能解释一下代码中的常量是什么吗? @Android WebLineindia
  • 它的类型应该是什么?它的显示无法解析符号常量。 @AndroidWeblineindia
  • 不要使用常量,我已经更新了我的答案,请检查。
  • String m_timeStamp = new SimpleDateFormat(Constant.TIME_STAMP_FORMAT, Locale.getDefault()).format(new java.util.Date()); 它在您的更新答案中 - @AndroidWeblinindia
  • 以及应该在捕获的图像必须显示的其他活动上写什么。 @AndroidWeblinindia
【解决方案2】:

您收到NullPointerException 是因为当用户进入相机意图时,当他捕获图像时,当用户从相机意图返回时,托管相机意图的活动被破坏或重新创建。 Duw picUri(从相机意图创建的图像路径)返回 null,因为现在内存中没有这样的对象。

像这样保存图片的路径:

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {

    if(mImageCaptureUri!=null)
        savedInstanceState.putString("camera_image", picUri.toString());

    super.onSaveInstanceState(savedInstanceState);
}

并从中检索图像路径:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("camera_image")) {
            picUri = Uri.parse(savedInstanceState.getString("camera_image"));
        }
    }

    super.onRestoreInstanceState(savedInstanceState);
}

引用自已回答的question

【讨论】:

  • sorry m new in android,我没有使用这两种方法,你能解释一下如何使用和放在哪里吗?如果我们使用它,那么不需要使用 onActivityResult 方法吗? -@vipul_asri
  • 不,这些方法是对onActivityResult() 方法的补充。您必须将这些方法放入 Activity 中,就像 onActivityResult()
猜你喜欢
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多