简体   繁体   English

相机意图无法将图像保存到图库

[英]Camera Intent doesn't save image to gallery

I'm a newbie android developer.Now I found an error when I compiled my camera application. 我是一名新手android开发人员。现在在编译相机应用程序时发现错误。

Actually the camera intent is working properly and the image taken can be shown on ImageView, but it isn't saved automatically to my phone gallery. 实际上,相机的意图可以正常工作,并且所拍摄的图像可以显示在ImageView上,但是不会自动保存到我的手机图库中。 After research, it took 3 days after picture taken to get saved in the image gallery. 经过研究,拍摄照片后花了3天时间将其保存在图库中。

I already tried some different methods of camera intent, but until now I have no significant results. 我已经尝试了几种不同的相机意图方法,但是直到现在我还没有取得明显的成果。 Any help will be so much appreciated, thank you. 任何帮助将不胜感激,谢谢。

my target SDK is 27 API , I already made an XML file for provider_paths and describe it in my manifests . 我的target SDK is 27 API ,我已经为provider_paths创建了XML文件,并在manifests中对其进行了描述。

here is my camera intent: 这是我的摄影意图:

public static final int REQUEST_CAMERA = 100;
Uri fileUri;
public final int SELECT_FILE = 11;

int bitmap_size = 40; // image quality 1 - 100;
int max_resolution_image = 800;

private void openCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, REQUEST_CAMERA);
}

public Uri getOutputMediaFileUri() {
    return FileProvider.getUriForFile(LaporActivity.this,
            BuildConfig.APPLICATION_ID + ".fileprovider",
            getOutputMediaFile());
}

private static File getOutputMediaFile() {

    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "KSD");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.e("Monitoring", "Oops! Failed create Monitoring directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_KSD_" + timeStamp + ".jpg");

    return mediaFile;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("onActivityResult", "requestCode " + requestCode + ", resultCode " + resultCode);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            try {
                Log.e("CAMERA", fileUri.getPath());

                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
                setToImageView(getResizedBitmap(bitmap, max_resolution_image));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == SELECT_FILE && data != null && data.getData() != null) {
            try {
                //  choose picture from Gallery
                bitmap = MediaStore.Images.Media.getBitmap(LaporActivity.this.getContentResolver(), data.getData());
                setToImageView(getResizedBitmap(bitmap, max_resolution_image));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

private void setToImageView(Bitmap bmp) {
    //compress image
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
    decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));

    //shows chosen picture from gallery to imageview
    fotoCaptured.setImageBitmap(decoded);
}

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float) width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

I expected the picture to get saved automatically to my gallery so I can continue to upload-process method using Retrofit, but the actual is when I clicked upload-button, the toast shows "IMGxxxxx No such file/directory" because the image path is not properly saved. 我希望图片会自动保存到我的画廊,因此我可以继续使用Retrofit进行上传处理,但是实际是当我单击“上传”按钮时,吐司显示“ IMGxxxxx No such file / directory”,因为图像路径为保存不正确。

Your file exist in memory but not on disk. 您的文件存在于内存中,但不在磁盘上。 Create the file before returning the object 返回对象之前创建文件

private static File getOutputMediaFile() { 
   ...
   ...
   //create file on disk
   if(!mediaFile.exists()) mediaFile.createNewFile();

   return mediaFile;
}

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

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