如何在android中的CustomAdapter类中调用相机意图?

时间:2015-04-23 07:07:19

标签: java android

我有一个名为CustomListAdapter类的Adapter类我是这样编写的,所以这里我得到一个问题类转换异常任何人都建议我

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = inflater.inflate(R.layout.myplace, null);

    final ImageView ivCamera = (ImageView)convertView.findViewById(R.id.ivCamera);

    ivCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
            adb.setTitle("Do u Want to Take Photo");
            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    count++;
                    String file = dir+count+".jpg";
                    File newfile = new File(file);
                    try {
                        newfile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    Uri outputFileUri = Uri.fromFile(newfile);

                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                    ((Activity) context).startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

                }
            });

            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

            adb.show();
        }
    });
}

我遇到了类强制转换异常请建议我如何解决此问题

在我的活动中

我称之为此方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    customListAdapter.onActivityResult(requestCode, resultCode, data);
    Bitmap bp = (Bitmap) data.getExtras().get("data");
    ivCamera.setImageBitmap(bp);
}

3 个答案:

答案 0 :(得分:1)

试试这个:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_PHOTO_CODE:
                if(resultCode == Activity.RESULT_OK) {
                    customListAdapter.onActivityResult(requestCode, resultCode, data);
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            ivCamera.setImageBitmap(bp);
                }
                break;
        }
    }

答案 1 :(得分:0)

http://developer.android.com/training/camera/photobasics.html

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

1)你没有检查 - 可能onActivityResult()在为结果开始一些其他活动后触发

2)resultCode == RESULT_OK&& extras!= null&& extras.get(" data")!= null可能很关键

3)尝试在调试模式下运行接收代码 - 某些设备可能无法传递位图(例如,因为它非常大)。如果是这样 - 尝试提供photoURI,然后在显示之前调整照片大小

4)尝试找到获取照片的库

答案 2 :(得分:0)

是的,Android中有两种类型的Context。其中1个是Activity-type,另一个是应用程序Context。 显然,您应该将适配器引用传递给您的Activity,即新的Adapter(YourActivity.this),而不是新的Adapter(getApplicationContext())

回答问题 - 如果使用getApplicationContext()启动结果活动,那么应该在onActivityResult()上执行什么活动.startActivityForResult()? - 一切都变得明显。