1

i have 5 files in [assets>files_new] folder

  • one.png
  • two.zip
  • three.rar
  • four.exe
  • five.txt

i want copy them to this directory (Internal Shared storage > Android > data > com.test.com) folder i test many codes but it was move files to sdcard.

MainActivity.java:

public class MainActivity extends Activity {

private static final String TAG = "MainActivity.java";
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   // your sd card
    String sdCard = Environment.getExternalStorageDirectory().toString();

    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/five.txt");

    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/five.txt");


     try {

        // 1 = move the file, 2 = copy the file
        int actionChoice = 2;

        // moving the file to another directory
        if(actionChoice==1){

            if(sourceLocation.renameTo(targetLocation)){
                Log.v(TAG, "Move file successful.");
            }else{
                Log.v(TAG, "Move file failed.");
            }

        }

        // we will copy the file
        else{

            // make sure the target file exists

            if(sourceLocation.exists()){

                InputStream in = new FileInputStream(sourceLocation);
                OutputStream out = new FileOutputStream(targetLocation);

                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();

                Log.v(TAG, "Copy file successful.");

            }else{
                Log.v(TAG, "Copy file failed. Source file missing.");
            }

        }

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }}}

here i try to move 1 file, but i looking for copy/move all files [assets>files_new] to directory.

my bad if i didn't explained that much. but i'm beginner. - i found this thread but not what i search on, Android - Copy assets to internal storage, i looking for copy all files from [assets>files_new] to directory which i mentioned before

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bego.
  • 29
  • 2
  • 7

0 Answers0