简体   繁体   中英

SdCard path correct path in android?

I don't know if the problem is the incorrect sdcard path or something else, but in my application i have a button that onClick it create a copy of the app folder in the sdcard (of course needs root). To me it works, i have stock android 4.4 with nexus 5 and it creates a folder named BackupApps in where there is the folder app with all apk files but some users tells me that the folder is not created. Here the code:

first; i created the path in sdcard with the new folder that will created:

final File customfolder=new File(Environment.getExternalStorageDirectory().toString()+File.separator+"BackupApps");

then the onClick code:

copy.setOnClickListener(new OnClickListener() {         
            public void onClick(View v){        
                        Process checkroot = null;
                        String rootcheck ="su";
                        String cd = "cd /";
                        String comando ="su cd cp -av /data/app "+customfolder;
                        customfolder.mkdir();
                        try {
                            Process copy = Runtime.getRuntime().exec(comando);
                            Toast.makeText(getActivity(), appmanagerfragment.this.getResources().getString(R.string.toastcopysi), 
                                    Toast.LENGTH_LONG).show();
                            Log.v("All In One Copia tag","Ok, folder copied in "+customfolder);

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }                  

        });

any idea?

I do not think your commando is correct, becuse su cd cp... makes no real sense. You should remove cd from commando

At first, there is a constructor for File, File(String,String) , that adds the separator by itself.

At second, I'm not sure there's su in a non-rooted android. In addition, some command line keys may not be supported in Android, for example, ls -l works, but 'ls -lF' does not. I suggest you run adb shell and perform the commands from the command line; when you know they do what you want, you move them to the code.

BTW, shell commands in the command line are separated with ; .

In general, I if the task is to copy a directory, I suggest (pardon) searching google with "copy directory java" or "copy directory android stackoverflow" and borrowing someone else's code. For example, this or this . And add the permission to write to sd card to your application. (BTW, this may be the reason why the subprocess failed.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM