• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

HELP IN CODING

aio developers

Android Enthusiast
pls who konws rhe code in android app programming for both main.xml and acivity to.do this
1.copy files
2. copy.folders
3.replace a specific name of file from one directory to another

pls this is needed
 
how to fix
 

Attachments

  • Screenshot_2019-01-07-23-09-51.png
    Screenshot_2019-01-07-23-09-51.png
    78.6 KB · Views: 107
File name looks odd MAINACTIVITY.JAVA.
Java is case sensitive and class MainActivity should be in a file called MainActivity.java
 
Check to see if you have the layout set in your onCreate() method. Something like this...
Java:
setContentView(R.layout.activity_main);
 
tried let me snd all the codr
 

Attachments

  • Screenshot_2019-01-08-00-48-21.png
    Screenshot_2019-01-08-00-48-21.png
    44.1 KB · Views: 107
  • Screenshot_2019-01-08-00-48-13.png
    Screenshot_2019-01-08-00-48-13.png
    81.9 KB · Views: 82
  • Screenshot_2019-01-08-00-32-04.png
    Screenshot_2019-01-08-00-32-04.png
    72 KB · Views: 112
  • Screenshot_2019-01-07-23-09-51.png
    Screenshot_2019-01-07-23-09-51.png
    78.6 KB · Views: 79
AND THE RESR code for the Mainctivity.java
 

Attachments

  • Screenshot_2019-01-08-00-51-29.png
    Screenshot_2019-01-08-00-51-29.png
    70.8 KB · Views: 111
  • Screenshot_2019-01-08-00-51-29.png
    Screenshot_2019-01-08-00-51-29.png
    70.8 KB · Views: 115
Start by creating a basic project in Android Studio using the 'blank project' template. This will create all the necessary project structure and configuration files.
You should be aware that an app on an unrooted device only has access to very specific parts of the filesystem.

In terms of file copying, some pointers here https://stackoverflow.com/questions/9292954/how-to-make-a-copy-of-a-file-in-android
sir Need the code
and explain for copy more than 2 files with folders too
and zipping files on a specified folder location
 
Ok in your MainActivity.java change the following from...
Java:
setContentView(R.layout.activity_main);

Change it to this...
Java:
setContentView(R.layout.main);
 
WHAT JUST HAPPENED

OwO
 

Attachments

  • Screenshot_2019-01-08-01-01-07.png
    Screenshot_2019-01-08-01-01-07.png
    86.8 KB · Views: 84
  • Screenshot_2019-01-08-01-01-34.png
    Screenshot_2019-01-08-01-01-34.png
    75.4 KB · Views: 126
app crashes :-(

That's because the main.xml needs a proper layout for your MainActivity. If you switch it back to activity_main then your MainActivity will not have a layout.

Just to be sure. Is there a layout file called activity_main.xml?
 
That's because the main.xml needs a proper layout for your MainActivity. If you switch it back to activity_main then your MainActivity will not have a layout.

Just to be sure. Is there a layout file called activity_main.xml?
and see the pic too of how i change it too
 

Attachments

  • Screenshot_2019-01-08-01-17-14.png
    Screenshot_2019-01-08-01-17-14.png
    56.1 KB · Views: 104
i want to code a button that at a click it replace files from one location to another but it just hard abd also to zip the file in a specific directory in a click of another button
Any code for that @GameTheory
 
Sorry I'm working on a couple apps at the moment so I'm in and out of the forums.

Do you have a way of posting code instead of screenshots? Because it's gonna very hard for anyone to help you this way and gonna take very long.
 
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button

        android:layout_height="wrap_content"
        android:text="PORT"
        android:layout_width="wrap_content"
        android:id="@+id/main"
        android:layout_below="@+id/main" />

</LinearLayout>
THE ABOVE IS MAIN.XML
the one below is MainActivity.java
Code:
package com.speedevs.mtkporting;

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

    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.app.Activity;


    public class MainActivity extends Activity {

        private static final String TAG = "MainActivity.java";

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

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

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

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

            // just to take note of the location sources
            Log.v(TAG, "sourceLocation: " + sourceLocation);
            Log.v(TAG, "targetLocation: " + targetLocation);

            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();
            }
        }
    }
 
ok let me post code

Ok so the issue before was that you had all your code in the onCreate() method which made your app crash. So I moved some things around just to get your app at least running. I don't know if your code works for copying files. You can try it and make whatever adjustments needed. I think you may have to request permissions at run time to copy files.

Java:
public class MainActivity extends AppCompatActivity {

    // your sd card
    String sdCard = Environment.getExternalStorageDirectory().toString();
    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/sample.txt");
    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

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

    public void copyFile(View view) {
        try {
            copy(sourceLocation, targetLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dst);
            try {
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

}

XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="copyFile"
        android:text="COPY FILE" />

</LinearLayout>

Well it's late now so I'll swing by here tomorrow.
 
Ok so the issue before was that you had all your code in the onCreate() method which made your app crash. So I moved some things around just to get your app at least running. I don't know if your code works for copying files. You can try it and make whatever adjustments needed. I think you may have to request permissions at run time to copy files.

Java:
public class MainActivity extends AppCompatActivity {

    // your sd card
    String sdCard = Environment.getExternalStorageDirectory().toString();
    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/sample.txt");
    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

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

    public void copyFile(View view) {
        try {
            copy(sourceLocation, targetLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dst);
            try {
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

}

XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="copyFile"
        android:text="COPY FILE" />

</LinearLayout>

Well it's late now so I'll swing by here tomorrow.
package com.speedevs.mtkporting; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io_OutputStream; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.app.Activity; public class MainActivity extends AppCompatActivity { // your sd card String sdCard = Environment.getExternalStorageDirectory().toString(); // the file to be moved or copied File sourceLocation = new File (sdCard + "/sample.txt"); // make sure your target location folder exists! File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt"); @override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void copyFile(View view) { try { copy(sourceLocation, targetLocation); } catch (IOException e) { e.printStackTrace(); } } public static void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); try { OutputStream out = new FileOutputStream(dst); try { // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { out.close(); } } finally { in.close(); } } }
Ok so the issue before was that you had all your code in the onCreate() method which made your app crash. So I moved some things around just to get your app at least running. I don't know if your code works for copying files. You can try it and make whatever adjustments needed. I think you may have to request permissions at run time to copy files.

Java:
public class MainActivity extends AppCompatActivity {

    // your sd card
    String sdCard = Environment.getExternalStorageDirectory().toString();
    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/sample.txt");
    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

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

    public void copyFile(View view) {
        try {
            copy(sourceLocation, targetLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dst);
            try {
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

}

XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="copyFile"
        android:text="COPY FILE" />

</LinearLayout>

Well it's late now so I'll swing by here tomorrow.
this is the code i use for main activity.java but errors
Code:
package com.speedevs.mtkporting;

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

    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.app.Activity;

public class MainActivity extends AppCompatActivity {

    // your sd card
    String sdCard = Environment.getExternalStorageDirectory().toString();
    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/sample.txt");
    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

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

    public void copyFile(View view) {
        try {
            copy(sourceLocation, targetLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dst);
            try {
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

}

and if uhave any code to copy file from one directory to another to zip and unizp files and also to copy folders i am ok
errors in pictures
 

Attachments

  • Screenshot_2019-01-08-03-43-58.png
    Screenshot_2019-01-08-03-43-58.png
    89.7 KB · Views: 105
  • Screenshot_2019-01-08-03-44-50.png
    Screenshot_2019-01-08-03-44-50.png
    86.6 KB · Views: 101
  • Screenshot_2019-01-08-03-44-56.png
    Screenshot_2019-01-08-03-44-56.png
    89.6 KB · Views: 75
  • Screenshot_2019-01-08-03-45-03.png
    Screenshot_2019-01-08-03-45-03.png
    102.8 KB · Views: 79
Change your imports to this...

Java:
package com.speedevs.mtkporting;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
Back
Top Bottom