Skip to content Skip to sidebar Skip to footer

How To Copy Image To An Existing Directory On Sd Card?

I'm trying to copy an image file with this code: InputStream fileInputStream = null; OutputStream fileOutputStream = null; String inPath = '/storage/emulated/0/Pictures/MyImage.jpg

Solution 1:

You need to create the file first, FileOutputStream will throw that exception if the file does not exist FileNotFoundException

FileoutputFile=newFile(outPath);
file.createNewFile();
fileOutputStream = newFileOutputStream(outputFile);

Solution 2:

if you are using targetSdkVersion 23 (or higher) in your app gradle file, you need to explicitly request the permission (could be into the onCreate method of the Activity or a button listener method), like this...

privatestaticfinalint CODE_WRITE_EXTERNAL = 10;
    if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    Log.d(TAG, "onCreate: " + "Show explanation");
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_WRITE_EXTERNAL );
                } else {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_WRITE_EXTERNAL );
                }
            } else {
                Log.d(TAG, "onCreate: " + "Permission already granted!");
                //Call your method to save the file
            }

Then you need to implement the next callback method

@OverridepublicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case CODE_WRITE_EXTERNAL : {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: Good to go!");
                    //Call your mehotd here
                } else {
                    Log.d(TAG, "onRequestPermissionsResult: Bad user");
                }
            }
        }
    }

Solution 3:

This is the way I used SAF to get the job done.

privatevoidnewcopyFile(File fileInput, String outputParentPath,
                    String mimeType, String newFileName) {

DocumentFiledocumentFileGoal= DocumentFile.fromTreeUri(this, treeUri);

String[] parts = outputParentPath.split("\\/");
for (inti=3; i < parts.length; i++) { //ex: parts:{"", "storage", "extSdCard", "MyFolder", "MyFolder", "MyFolder"}if (documentFileGoal != null) {
        documentFileGoal = documentFileGoal.findFile(parts[i]);
    }
}
if (documentFileGoal == null) {
    Toast.makeText(MainActivity.this, "Directory not found", Toast.LENGTH_SHORT).show();
    return;
}

DocumentFiledocumentFileNewFile= documentFileGoal.createFile(mimeType, newFileName);

InputStreaminputStream=null;
OutputStreamoutputStream=null;
try {
    outputStream = getContentResolver().openOutputStream(documentFileNewFile.getUri());
    inputStream = newFileInputStream(fileInput);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
try {
    if (outputStream != null) {
        byte[] buffer = newbyte[1024];
        int read;
        if (inputStream != null) {
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
            }
        }
        if (inputStream != null) {
            inputStream.close();
        }
        inputStream = null;
        outputStream.flush();
        outputStream.close();
        outputStream = null;
    }
} catch (IOException e) {
    e.printStackTrace();
}
}

Post a Comment for "How To Copy Image To An Existing Directory On Sd Card?"