Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 无法通过4.2.2 AVD上的意图选择器从相机获取图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在处理我的应用程序的一部分,该应用程序允许用户使用Intent选择器从相机或图库中选择图像.

它在我的2.2.1安卓手机上工作正常,但是当我在4.2.2 AVD上编译它时,当我使用相机时它会返回空指针错误,

public void onClick(View view) 
{
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);


    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    Intent chooser = new Intent(Intent.ACTION_CHOOSER);
    chooser.putExtra(Intent.EXTRA_INTENT,galleryIntent);      
    chooser.putExtra(Intent.EXTRA_titlE,"Chooser");

    Intent[] intentArray =  {CameraIntent}; 
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,intentArray);
    startActivityForResult(chooser,requEST_CODE);
}

protected void onActivityResult(int requestCode,int resultCode,Intent data) 
{
    if (requestCode == requEST_CODE && resultCode == Activity.RESULT_OK)
    {
    try 
            {
            if (bitmap != null) 
                {
                    bitmap.recycle();
                }

            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            imageView.setImageBitmap(bitmap);
            }

        catch (FileNotFoundException E) 
            {
                e.printStackTrace();
            }

        catch (IOException E) 
            {
                e.printStackTrace();
            }
    super.onActivityResult(requestCode,resultCode,data);
    }
}

这是我得到的错误
05-05 05:03:31.730:E / AndroidRuntime(820):引起:java.lang.NullPointerException

并且它说问题在于这一行:

InputStream stream = getContentResolver().openInputStream(data.getData());

我究竟做错了什么?

更新:

现在它正在运行,这是解决方案:

protected void onActivityResult(int requestCode,Intent data) 
{
    if (requestCode == requEST_CODE && resultCode == Activity.RESULT_OK)
    {
        if(data.getData()!=null)
        {
            try 
            {
            if (bitmap != null) 
                {
                    bitmap.recycle();
                }

            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            imageView.setImageBitmap(bitmap);
            }

        catch (FileNotFoundException E) 
            {
                e.printStackTrace();
            }

        catch (IOException E) 
            {
                e.printStackTrace();
            }
        } 

        else 
        {
            bitmap=(Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(bitmap);
        }

        super.onActivityResult(requestCode,data);
    }
}

谢谢你的帮助!

解决方法

您好试试这段代码.

以下代码用于相机按钮点击工作:

imgviewCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 //define the file-name to save photo taken by Camera activity
                String filename = "new-photo-name.jpg";
                //create parameters for Intent with filename
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.titlE,fileName);
                values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
                //imageUri is the current activity attribute,define and save it for later usage (also in onSaveInstanceStatE)
                imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
                //create new Intent
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
                startActivityForResult(intent,PICK_Camera_IMAGE);
            }
        });


OnActivityresult code will be like below 



protected void onActivityResult(int requestCode,Intent data) {
        Uri SELEctedImageUri = null;
        String filePath = null;
        switch (requestCodE) {               
                case PICK_Camera_IMAGE:
                     if (resultCode == RESULT_OK) {
                        //use imageUri here to access the image
                        SELEctedImageUri = imageUri;

                    } else if (resultCode == RESULT_CANCELED) {
                        Toast.makeText(this,"Picture was not taken",Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this,Toast.LENGTH_SHORT).show();
                    }
                     break;
            }

            if(SELEctedImageUri != null){
                    try {
                        // OI FILE Manager
                        String filemanagerString = SELEctedImageUri.getPath();

                        // MEDIA GALLERY
                        String SELEctedImagePath = getPath(SELEctedImageUri);

                        if (SELEctedImagePath != null) {
                            filePath = SELEctedImagePath;
                        } else if (filemanagerString != null) {
                            filePath = filemanagerString;
                        } else {
                            Toast.makeText(getApplicationContext(),"UnkNown path",Toast.LENGTH_LONG).show();
                            Log.e("Bitmap","UnkNown path");
                        }

                        if (filePath != null) {

                            Toast.makeText(getApplicationContext()," path" + filePath,Toast.LENGTH_LONG).show();

                        Intent i = new Intent(getApplicationContext(),EditOractivity.class);
                            // passing array index
                            i.putExtra("id",filePath);
                            startActivity(i);

                        } 
                    } catch (Exception E) {
                        Toast.makeText(getApplicationContext(),"Internal error",Toast.LENGTH_LONG).show();
                        Log.e(e.getClass().getName(),e.getmessage(),E);
                    }
            }



    }

     public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            cursor cursor = managedQuery(uri,projection,null,null);
            if (cursor != null) {
                // HERE you wILL GET A NULLPOINTER IF cursOR IS NULL
                // THIS CAN BE,IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
                int column_index = cursor
                        .getcolumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_indeX);
            } else
                return null;
        }

别忘了在清单文件添加相机权限.希望这会帮助你.

大佬总结

以上是大佬教程为你收集整理的android – 无法通过4.2.2 AVD上的意图选择器从相机获取图像全部内容,希望文章能够帮你解决android – 无法通过4.2.2 AVD上的意图选择器从相机获取图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。