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

HELP IN CODING

Upvote 0
how to fix
 

Attachments

  • Screenshot_2019-01-07-23-09-51.png
    Screenshot_2019-01-07-23-09-51.png
    78.6 KB · Views: 82
Upvote 0
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
 
Upvote 0
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: 80
Upvote 0
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();
            }
        }
    }
 
Upvote 0
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.
 
  • Like
Reactions: aio developers
Upvote 0
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: 72
  • Screenshot_2019-01-08-03-44-50.png
    Screenshot_2019-01-08-03-44-50.png
    86.6 KB · Views: 69
  • Screenshot_2019-01-08-03-44-56.png
    Screenshot_2019-01-08-03-44-56.png
    89.6 KB · Views: 47
  • Screenshot_2019-01-08-03-45-03.png
    Screenshot_2019-01-08-03-45-03.png
    102.8 KB · Views: 46
Upvote 0
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;
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones