0

The code I have runs perfectly but the file is not copied to "/data/data/game" root folder where the game is located. In fact, it is copied to "/storage/emulated/0" Please help me, the file I want to copy is a simple .json, which just changes the text, it doesn't even modify values, etc.

package com.example.myapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSION_RQUEST_STORAGE = 1;

    Button button;

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

        //give the app permission to access storage
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_RQUEST_STORAGE);
            } else {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_RQUEST_STORAGE);
            }
        } else {
            //do nothing
        }

        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                copyAsset("000001-1.json");
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSION_RQUEST_STORAGE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ContextCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        //do nothing
                    }
                } else {
                    Toast.makeText(this, "No permission granted!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private void copyAsset(String filename) {
        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "//data/data/game";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        AssetManager assetManager = getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outfile = new File(dirPath, filename);
            out = new FileOutputStream(outfile);
            copyFile(in, out);
            Toast.makeText(this, "¡Parchado!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "¡Error!", Toast.LENGTH_SHORT).show();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    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);
        }
    }
}

there is no errors(meaning that my app runs), I just want it to be copied to "data/data/game"

Blank
  • 1
  • 1
  • Your not getting any errors because you are not checking for them. See [How to make a copy of a file in android?](https://stackoverflow.com/questions/9292954/how-to-make-a-copy-of-a-file-in-android) – Jon Goodwin Sep 17 '19 at 01:34
  • but I just want to change the path to root directory, my app runs, but the files are not copied into /data/data – Blank Sep 17 '19 at 02:13
  • 1
    `Environment.getExternalStorageDirectory()` will give you the external storage directory, not the `data/data`. Files of which app do you want to modify? Your own app? – Vladyslav Matviienko Sep 17 '19 at 05:38
  • No, it's a game, I just changed some text of a file, it was easy to edit them cause they were .json, but I'm struggling here – Blank Sep 17 '19 at 15:26
  • You cannot **write** files *anywhere* you want (*unless* you are **root** Android are **very keen** on this). You need **permission** (file system permission...), if you don't have it, you cannot do it. But you can **check** why your write failed (which you do not). – Jon Goodwin Sep 21 '19 at 21:15

0 Answers0