-2

i have retrieve all apk installed in my device and I created a list in which I can see them. On click over an item, I have created a dialog.I would that on positive button copy the apk of that position in a folder I created in the sdcard. So far I tried and I can show in a toast the app folder and position of each apk but I don't understand how can I copy that apk I have selected. This is the code

.setPositiveButton("Copy apk", new DialogInterface.OnClickListener() {

               @Override
               public void onClick(DialogInterface dialogapk, int id)
               {
                   // TODO: Implement this method
                   final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                   mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                   final List pkapplist = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);

                   for (Object o : pkapplist) {
                       ResolveInfo info = (ResolveInfo) o;
                       file = new File( info.activityInfo.applicationInfo.publicSourceDir);
                       Toast.makeText(getActivity(), "Boh "+file, Toast.LENGTH_SHORT).show();

                   }
                   try
                   {    
                       process = Runtime.getRuntime().exec("cp " + file + " " + customfolder);
                   }
                   catch (IOException e)
                   {

                   }

               }


        });

Custom folder is the folder I have created in this way

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

Actually it creates a file and not a folder of size of application itself..I don't know exactly what it is doing!! Thanks. If needs the adapter let me know

Atlas91
  • 5,754
  • 17
  • 69
  • 141

2 Answers2

0

1) Did you declare "android.permission.WRITE_EXTERNAL_STORAGE" permission in your Manifest file?
2) cp command is missing on Android system commands: /system/bin/sh: cp: not found
3) When creating customfolder you are missing customfolder.mkdirs() statement to actually create this custom dir on the external storage
4) Try following solution

Update
Tried to implement your logic. Following solution (taken from aforementioned link) proved itself as working one (it actually backups APK files onto the /sdcard):

FileInputStream inStream = new FileInputStream(file.getAbsolutePath());
FileOutputStream outStream = new FileOutputStream(backupDir.getAbsolutePath() +  File.separator + file.getName());
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
Community
  • 1
  • 1
A.Vynohradov
  • 276
  • 1
  • 8
  • 1) Done, 2)i think it works, 3)mmh..maybe you're right, 4)No needs root to do this operation :) but however not wors :( – Atlas91 Jul 10 '14 at 07:45
0
File  Folder=new File(Environment.getExternalStorageDirectory(),"YourFolder");
        if(!Folder.exists()){
            Folder.mkdirs();
        }

This is the code to check n create

Meghna
  • 539
  • 4
  • 14