【问题标题】:Open camera take photo and set it in image view in PopupWindow - android打开相机拍照并将其设置在 PopupWindow 的图像视图中 - android
【发布时间】:2016-02-10 16:30:18
【问题描述】:

我在 Android 中有一个 PopupWindow,里面有一个 imageview。当我按下 imageview 时,我想打开相机拍照,当我回来为 imageview 的背景设置照片时。

问题是我回来的时候(onActivityResult),popupwindow是dismiss(),imageView背景是默认的。

imageView@onClick:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Log.i(TAG, "IOException");
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                }
            }

createImageFile() 函数

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();

    editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
    editor.putString("picture_path", mCurrentPhotoPath);
    editor.commit();

    return image;
}

@onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        try {

            prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
            mCurrentPhotoPath = prefs.getString("picture_path", null);

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            add_photo.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我使用 Sharedpreferences 来避免 mCurrentPhotoPath 上出现 null。

【问题讨论】:

    标签: android android-camera android-imageview popupwindow


    【解决方案1】:

    假设问题是 MediaStore.Images.Media.getBitmap 返回 null,这是因为它旨在用于从某些 ContentResolver 中的某些应用程序处理的 URL 中检索图像,而在您的实现中并非如此.

    在您的实现中,您将图像存储在设备文件系统上,您可以使用返回位图的BitmapFactorys decodeFile 方法检索它。

    【讨论】:

    • 我不得不说我已经在 Samsung S5 android 5.0 上测试了我的代码,并且它正在运行。但在其他设备上..
    • 你并没有真正解释你的问题,所以我假设 getBitmap 方法返回 null,是吗?
    • 不! mCurrentPhotoPath 为空,但我使用共享首选项来保存和恢复它的值,所以没关系。它不会改变 iMageVie 中的图片,这是我的问题。但是在 S5 上,我已经测试过了,它正在工作......头痛......
    • 所以在某些设备上它不起作用?因为它从共享首选项返回 null ?这可能是因为其他设备很可能没有 getExternalStoragePublicDirectory 可用,这意味着您需要使用内部存储目录。您是否确保它首先返回有效值?请尝试在 createImageFile 方法中首先记录您收到的文件路径
    • 我真的想不出为什么 nativeLockCanvas 没有成功,是在尝试设置位图时发生异常吗?但是OutOfMemoryError的原因正如错误名称所说,设备内存不足,这可能是因为您的应用程序中的内存泄漏,或者分配了太大的内存块,可以通过加载图像来克服在 ImageView 中以较小的分辨率为例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2015-03-07
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多