android - Android 中相机图像返回缩略图

标签 android android-image android-camera-intent onactivityresult android-bitmap

在我的 Android 应用程序中,我想从相机捕获图像并将该图像 Uri 传递到下一个 Activity 。

在这里,我得到了捕获图像的缩略图。为了解决这个问题,我使用 MediaStore.EXTRA_OUTPUT 来捕获图片。

现在,我不知道如何获取当前捕获图像的 uri。我尝试了下面的代码并在下一个 Activity 中传递 imageUri ,但它抛出 NoSuchFileDirectory 异常。

我提到了this链接,但对于获取捕获的图像 Uri 并将其传递到下一个 Activity 没有任何明确的想法。请帮我解决这个问题。如果您发现此代码有任何问题,请指出我哪里出错了。

代码:

        btnCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent pictureActionIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                File photo = null;
                try {

                    photo = SaveCameraImage();

                } catch (Exception e) {
                    Log.e("Camera capture-------------",
                            "Can't create file to take picture!");
                    Toast.makeText(SelectFrameActivity.this,
                            "Please check SD card! Image shot is impossible!",
                            10000).show();

                }
                mImageUri = Uri.fromFile(photo);
                pictureActionIntent
                        .putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

                startActivityForResult(pictureActionIntent, TAKE_PICTURE);
            }
        });


private File SaveCameraImage() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String dt = sdf.format(new Date());

            imageFile = null;
            imageFile = new File(Environment.getExternalStorageDirectory(),
                    "FrameFace/" + "Camera_" + dt + ".png");
            Log.e("New Camera Image Path:-",
                    Environment.getExternalStorageDirectory() + "/FrameFace/"
                            + "Camera_" + dt + ".png");

            if (!imageFile.exists())
                imageFile.createNewFile();

            bmp_camera_image.compress(Bitmap.CompressFormat.PNG, 100,
                    new FileOutputStream(imageFile));

            Log.e("imagetext", "the file has saved to the sdcard");

            imagePath = Environment.getExternalStorageDirectory()
                    + "/FrameFace/" + "Camera_" + dt + ".png";

            Log.e("FaceFrameActivity - SaveCameraImage: ", "Image is Saved to"
                    + imagePath);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return imageFile;
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode,
            final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
                    + Integer.toString(resultCode));

            if (resultCode == Activity.RESULT_OK) {

                // bmp_camera_image = (Bitmap) data.getExtras().get("data");
                SaveCameraImage();

                // this.getContentResolver().notifyChange(mImageUri, null);
                // ContentResolver cr = this.getContentResolver();
                // try {
                // GlobalClass.cameraBitmap =
                // android.provider.MediaStore.Images.Media
                // .getBitmap(cr, mImageUri);
                // } catch (FileNotFoundException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // } catch (IOException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // }

                Handler h = new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent = new Intent(getApplicationContext(),
                                FaceCropActivity_dual.class);
                        // intent.putExtra("imagePath", imagePath);
                        intent.putExtra("imagePath", mImageUri.toString());
                        startActivity(intent);
                    }
                }, 500);
            }
            break;
    }
    }

最佳答案

我使用了这段代码,它可以帮助我保存相机图像并获取该图像的 uri。

btnCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    dialog.dismiss();
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss");
                    String dt = sdf.format(new Date());

                    imageFile = null;
                    imageFile = new File(Environment
                            .getExternalStorageDirectory() + "/FrameFace/",
                            "Camera_" + dt + ".png");
                    Log.e("New Camera Image Path:-",
                            Environment.getExternalStorageDirectory()
                                    + "/FrameFace/" + "Camera_" + dt + ".png");

                    if (!imageFile.exists())
                        imageFile.createNewFile();

                    imagePath = Environment.getExternalStorageDirectory()
                            + "/FrameFace/" + "Camera_" + dt + ".png";

                    Uri outputFileUri = Uri.fromFile(imageFile);

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

                    pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            outputFileUri);

                    startActivityForResult(pictureActionIntent, TAKE_PICTURE);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode,
            final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
                    + Integer.toString(resultCode));

            if (resultCode == Activity.RESULT_OK) {
Handler h = new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent = new Intent(getApplicationContext(),
                                FaceCropActivity_dual.class);
                        // intent.putExtra("imagePath", imagePath);
                        Log.e("Image Path-------------", "" + imagePath);
                        intent.putExtra("imagePath", imagePath);
                        startActivity(intent);
                    }
                }, 500);
            }
            break;
}
    }

关于android - Android 中相机图像返回缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20945018/

相关文章:

android - 转动模拟器后布局发生变化,但不是设备

android - 对 searchView 使用 Android Studio 默认搜索图标

android - 在 ListView 中显示来自 URL 的图像

android - 将相机拍照 Intent 中的图像保存到应用程序私有(private)存储

java - UnsupportedOperationException 打开相机时出错 android

android - 如何转换音频文件中的录制视频或现有视频?

android - FragmentTransation setCustomAnimations 不起作用

java - 更改 vector 图像的色调颜色时出现 OutOfMemoryError 错误

android - 拍照 Intent ,不懂路径

java - 在 android 中存储长数据列表的最佳选择