简体   繁体   English

捕获的图像未保存在设备上

[英]Captured image does not get saved on the device

I am trying to save a camera captured image on the device. 我正在尝试将相机捕获的图像保存在设备上。 But it does not get saved. 但是它不会被保存。

//---manifest // - -表现

<uses-permission android:name="ANDROID.PERMISSION.CAMERA" />
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" />

I use the following code: 我使用以下代码:

    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            try {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "myimage.jpg");
                FileOutputStream fos = new FileOutputStream(outFile);
                photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                fos.flush();
                fos.close();
                getImages();

See what i have done in my code for saving the captured image. 查看我在代码中所做的保存捕获的图像的操作。

/*************************** Camera Intent Start ************************/

            // Define the file-name to save photo taken by Camera activity

            String fileName = "Camera_Example.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

            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            /**** EXTERNAL_CONTENT_URI : style URI for the "primary" external storage volume. ****/


            // Standard Intent action that can be sent to have the camera
            // application capture an image and return it.

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

            /*************************** Camera Intent End ************************/


        }

onActivityResult method is here: onActivityResult方法在这里:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

        if (resultCode == RESULT_OK) {

            /*********** Load Captured Image And Data Start ****************/

            String imageId = convertImageUriToFile(imageUri, CameraActivity);


            //  Create and excecute AsyncTask to load capture image

            new LoadImagesFromSDCard().execute("" + imageId);

            /*********** Load Captured Image And Data End ****************/


        } else if (resultCode == RESULT_CANCELED) {

            Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show();
        } else {

            Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show();
        }
    }
}

// don't use data.getData() as it return null in some device. //不要使用data.getData(),因为它在某些设备中返回null。

So use the cursor for getting captured Image. 因此,使用光标获取捕获的图像。

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

    if (resultData != null) {

    String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, null, null, null);
            int column_index_data = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToLast();

            String imagePath = cursor.getString(column_index_data);
            Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath );
            imageView.setImageBitmap(bitmapImage );

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM