简体   繁体   中英

i am not able to save the image throws IOException everytime

File creation code

SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault());
        String date = format.format(new Date());
        fileName = path + "/" + date + ".png";

        if (!path.exists())
        {
            path.mkdirs();
            Toast.makeText(MainActivity.this, "Saved at "+ String.valueOf(path), Toast.LENGTH_LONG).show();
        }

it always throws IOExecption because of that it catches the exception and always shows the TOAST message Couldn't Save Please help I am trying to resolve this issue for so long

            @Override
            public void onClick(View view) {
                if (!signatureView.isBitmapEmpty())
                {
                    try {
                        saveImage();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this, "Couldn't Save. Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    private void saveImage() throws IOException {

        File file = new File(fileName);

        Bitmap bitmap = signatureView.getSignatureBitmap();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
        byte[] bitmapData = bos.toByteArray();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bitmapData);
        fos.flush();
        fos.close();

        Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
    }

it always throws IOExecption because of that it catches the exception and always shows the TOAST message Couldn't Save Please help I am trying to resolve this issue for so long. It does not give any errors on android 10 or below but when I test it on android 11 ENOENT(No such file or directory) and when I manually create the painter file it gives me the error open failed EPERM (operation not permitted).

Kotlin version

Create a function to generate file

private fun getOutputMediaFile(): File? {
    return try {
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageFileName = "JPEG_" + timeStamp + "_"
        val storageDir: File = context.cacheDir
        File.createTempFile(
                imageFileName,  /* prefix */
                ".JPEG",  /* suffix */
                storageDir /* directory */
        )
    } catch (e: IOException) {
        null
    }
}

Function to save file

fun saveBitmap(bitmap: Bitmap){
    var fileOutputStream: FileOutputStream? = null
    try {
        fileOutputStream =  FileOutputStream(getOutputMediaFile())
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream) // save bitmap image
    } catch (e: IOException) {
        e.printStackTrace()
    }finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
}

Java version

Function to create a file

public static File createFile(Context context){
    String fileName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File storageDir = context.getCacheDir();
    File file = null;
    try {
         file = File.createTempFile(fileName,".JPEG", storageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
return file;
}

Function to save file

 public static void saveBitmap(Context context, Bitmap bitmap){
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(createFile(context));
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }finally {
        if (fileOutputStream != null){
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

You need to call saveBitmap() function and pass the required data in the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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