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

时间:2019-08-01 02:18:38

标签: java android android-camera-intent

我是一名新手android开发人员。现在,在编译我的相机应用程序时发现了一个错误。

实际上,相机的意图可以正常工作,并且所拍摄的图像可以显示在ImageView上,但不会自动保存到我的手机图库中。经过研究,拍摄照片后花了3天时间将其保存在图库中。

我已经尝试了一些不同的相机意图方法,但是直到现在我还没有取得明显的成果。任何帮助将非常感谢,谢谢。

我的target SDK is 27 API,我已经为provider_paths制作了XML文件,并在我的manifests中对其进行了描述。

这是我的相机意图:

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);
}

我希望图片会自动保存到我的画廊,因此我可以继续使用Retrofit进行上传处理,但是实际上是当我单击“上传”按钮时,吐司显示“ IMGxxxxx No such file / directory”,因为图片路径未正确保存。

1 个答案:

答案 0 :(得分:0)

您的文件存在于内存中,但不在磁盘上。在返回对象之前创建文件

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

   return mediaFile;
}