Monday, 27 June 2022

How to move/copy any type of file from asset file to scoped storage ANDROID Q in JAVA?

I have already succeeded with this operation with images, but I cannot do it with other type of file, in my case I try to insert a database.

Here is an example of the code for the images:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
        try {
            try {
                pictures = assetManager.list("photos/dataset1");
            } catch (IOException e) {
                Log.e("tag", "Failed to get asset file list.", e);
            }
            if (pictures != null) {
                for (String filename : pictures) {
                    InputStream in;
                    OutputStream out;
                    InputStream inputStream = assetManager.open("photos/dataset1/"+filename);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    saveImageToGallery(bitmap);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

This method below works for the images :

public void saveImageToGallery(Bitmap bitmap) {
    OutputStream outputStream;
    Context myContext = requireContext();
    try {
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.Q){
            ContentResolver contentResolver = requireContext().getContentResolver();
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME,"Image_"+".jpg");
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
            Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            outputStream = contentResolver.openOutputStream(Objects.requireNonNull(imageUri));
            bitmap.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
            Objects.requireNonNull(outputStream);

        }
    }catch (FileNotFoundException e) {

        e.printStackTrace();
    }
}

and there my try for the other type of file :

 if (files!= null) {
        for (String filename : files) {
            InputStream in;
            OutputStream out;
            try {
                in = assetManager.open("database/test/" + filename);
                File outFile = new File(databasesFolder, filename);
                out = new FileOutputStream(outFile);
                copyFile(in, out);
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            }
        }
    } else {
        Log.e("Error NPE", "files is null");
    }



    private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

This function above is not working, I want something like this or a function similary as the function for my images but for any type of file. When I run my application I have no error however nothing happens



from How to move/copy any type of file from asset file to scoped storage ANDROID Q in JAVA?

No comments:

Post a Comment