How to copy files to another path(Download to Data folder) from your Android App? -Android 12 API 32

Veröffentlicht am: 03 November 2022
auf dem Kanal: Programmer World
4,434
40

This video shows the steps to read the content of file from one location and copy it to another location from your Android App.

It reads an image file from Download folder and then writes to app data folder. It uses inputstream.read to read the file content and outputstream.write to copy the data to the new file.


I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...


However, the main Java code is copied below also for reference:


package com.programmerworld.copyfilesapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);

textView = findViewById(R.id.textView);
}


public void buttonCopyFiles(View view){
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
List ANGULAR_BRACKET StorageVolume ANGULAR_BRACKET storageVolumeList = storageManager.getStorageVolumes();
StorageVolume storageVolume = storageVolumeList.get(0);
File fileSource =new File(storageVolume.getDirectory().getPath()+"/Download/Image.jpeg");
File fileDestination = new File(getExternalFilesDir(null), "Image_Destination1.jpeg");
try {
InputStream inputStream = new FileInputStream(fileSource);
OutputStream outputStream = new FileOutputStream(fileDestination);
byte[] byteArrayBuffer = new byte[1024];
int intLength;
while ((intLength = inputStream.read(byteArrayBuffer)) LESS_THAN 0){
outputStream.write(byteArrayBuffer, 0, intLength);
}
inputStream.close();
outputStream.close();

textView.setText("SUCCESS");
}catch (Exception e){
textView.setText("ERROR: " + e.toString());
}
}
}


-


Auf dieser Seite können Sie das Online-Video How to copy files to another path(Download to Data folder) from your Android App? -Android 12 API 32 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Programmer World 03 November 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 4,434 Mal angesehen und es wurde von 40 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!