0

我正在尝试在我的应用程序中使用相机类。我只想单击一张图片并在 imageview 上进行设置,然后将其发布在某个 url 上。在 url 上发布工作正常,但有时在单击任何图片并从我导航到相机应用程序的位置恢复到相同的活动时会出现问题。它在 HTC 野火(2.2 版)上运行良好,但有时会出现异常(失败几率 1/25),但是当我在索尼 xperia miro 或三星标签(4.0 版)上测试它时,它会多次给出相同的异常(失败几率 20/25) . 我没有找到问题所在,因为有时应用程序运行平稳,没有任何异常,但使用 4.0 或更高版本时,它会强制关闭很多次,但有时可以正常运行。

例外是:java.lang.RuntimeException: Unable to resume activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=tabHome, request=1, result=-1, data=Intent { dat=content://media/external/images/media/17271 }} to activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}: java.lang.ClassCastException: fable.eventippo.Home cannot be cast to fable.eventippo.AddEvent

这里给出了完整的代码。

按钮 Onclick。

browse = (Button) findViewById(R.id.browse);
    browse.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final CharSequence[] items = { "Camera", "Gallery" };
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getParent());
            builder.setTitle("Browse From");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    if (items[item] == "Camera") {
                        PackageManager pm = getPackageManager();

                        if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                            Intent i = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                            i.putExtra(MediaStore.EXTRA_OUTPUT,
                                    MyFileContentProviders.CONTENT_URI);

                            getParent().startActivityForResult(i,
                                    CAMERA_REQUEST);

                        } else {

                            Toast.makeText(getParent(),
                                    "Camera is not available",
                                    Toast.LENGTH_LONG).show();

                        }
                    } else if (items[item] == "Gallery") {
                        try {

                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            getParent().startActivityForResult(
                                    Intent.createChooser(intent,
                                            "Select Picture"),         PICK_IMAGE);
                        } catch (Exception e) {
                            Toast.makeText(getParent(), e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                            Log.e(e.getClass().getName(), e.getMessage(), e);
                        }

                    }
                }

            });
            AlertDialog alert = builder.create();
            alert.show();

        }
    });

关于活动结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Bundle bn = data.getExtras();
    switch (requestCode) {
    case PICK_IMAGE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String filepath = null;

            try {
                // OI FILE Manager
                String filemanagerstring = selectedImageUri.getPath();
                // MEDIA GALLERY
                String selectedImagePath = getPath(selectedImageUri);
                // logo.setImageURI(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) {
                    // /upload.setText(filepath);
                    decodeFile(filepath);
                } else {
                    // filePath = null;
                    bitmap = null;
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error",
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }
        }
        break;
    case CAMERA_REQUEST:
        if (resultCode == Activity.RESULT_OK) {

            File out = new File(getFilesDir(), "newImage.jpg");

            if (!out.exists()) {

                Toast.makeText(getBaseContext(),

                "Error while capturing image", Toast.LENGTH_LONG)

                .show();

                return;

            }

            String filepath = out.getAbsolutePath().toString();
            decodeFile(filepath);

        }
        break;
    default:
    }

下图显示了完整的异常此处显示异常

如果您需要更多信息,请告诉我。

提前致谢

等待答复

4

1 回答 1

1

点击监听器上的拍照按钮:

TakePicture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyPic-"+ System.currentTimeMillis() + ".jpg");
            SelectedImage = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, SelectedImage);
            startActivityForResult(intent,CAMERA_PIC_REQUEST);

        }
    });

选择从库按钮 onClickListener:

SelectfromGallery.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             Intent intent = new Intent();
             intent.setType("image/*");
             intent.setAction(Intent.ACTION_GET_CONTENT);
             startActivityForResult(Intent.createChooser(intent,"Select Picture"), RESULT_LOAD_IMAGE);

        }
    });

活动结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

        SelectedImage = data.getData();
        String filePath = null;

        try {
            // IO FILE Manager
            String filemanagerstring = SelectedImage.getPath();

            // MEDIA GALLERY
            String selectedImagePath = getPath(SelectedImage);

            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) {
                decodeFile(filePath);   
            } else {
                bitmap = null;
            }
            FROM_GALLERY = true;

        } catch (Exception e) {
            Log.e("Uploaderror", e.getLocalizedMessage());
        }
    }
    else if(requestCode==CAMERA_PIC_REQUEST && resultCode == RESULT_OK){

        /*//SelectedImage = data.getData();
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ProfilePic.setImageBitmap(thumbnail);*/
        String filePath = null;
        try {
            // OI FILE Manager
            String filemanagerstring = SelectedImage.getPath();

            // MEDIA GALLERY
            String selectedImagePath = getPath(SelectedImage);

            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) {
                decodeFile(filePath);
                FROM_GALLERY = false;
            } else {
                bitmap = null;
            }

        } catch (Exception e) {
            Log.e("error:", e.getLocalizedMessage());
        }

    }
    else if (resultCode == Activity.RESULT_CANCELED) 
      {
        Log.e("STATUS:", "Selecting picture cancelled"); 
      } 
}

解码文件方法:

   public void decodeFile(String filePath) {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);

    ProfilePic.setImageBitmap(bitmap);
}

干得好。我实现该目标的完整代码。由于设备有限,我无法进行彻底的测试。因此,不能肯定地说这适用于所有设备。如果您找到问题的解决方案或者此代码是否有效,请告诉我。谢谢。快乐编码。

于 2013-05-16T11:19:02.213 回答