0

I am trying to save an Imgefile from a picture taken from the camera with high resolution. Unfortunatly no file is beeing created. I don't know where the error lies.

public void saveImage(Bitmap imageFile) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "Image_" + timeStamp;

try {
String path = getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString();
File saveImage = new File(path + File.separator + imageName + ".jpg");
FileOutputStream out = new FileOutputStream(saveImage);
imageFile.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();

} catch (IOException e) {
e.printStackTrace();
}
}

In AndroidManifest.xml i have set the permission to write to external storage.

<manifest>
[...]
    <uses-feature
        android:name="android.hardware.Camera"
        android:required="false" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.Camera.autofocus" />

    <application
       [...]>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_path" ></meta-data>
        </provider>
[...]
</manifest>
4

1 回答 1

1

在写入之前创建新文件(因为FileOutputStream需要打开现有文件):

File saveImage = new File(path + File.separator + imageName + ".jpg");
saveImage.createNewFile();  /// <--- add this line
FileOutputStream out = new FileOutputStream(saveImage);
于 2019-07-21T15:03:05.570 回答