在Android中以编程方式将所有安装apk文件备份到sdcard中

时间:2014-01-11 04:30:54

标签: android copy apk android-sdcard android-install-apk

我想在我的SD卡中备份我的所有安装应用程序,所以我写下代码

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
    for (Object object : pkgAppsList) {
        ResolveInfo info = (ResolveInfo) object;
        File file =new File( info.activityInfo.applicationInfo.publicSourceDir);
        InputStream myInput;
        System.out.println("SDSD "+file.toString());

        String sdpath = Environment.getExternalStorageDirectory().getPath();
        try {
            File exist=new File(Environment.getExternalStorageState()+file.toString());
            System.out.println("1 "+ exist.exists());
            // Set the output folder on the Scard
            File directory = new File(sdpath + "/Back");
            // Create the folder if it doesn't exist:
            if (!directory.exists()) {
            directory.mkdirs();
            }
            // Set the output file stream up:
            myInput = new FileInputStream(Environment.getExternalStorageState()+ file.toString());
            OutputStream myOutput = new FileOutputStream(directory.getPath()+ file.toString());
            // Transfer bytes from the input file to the output file
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
            }
            // Close and clear the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
            Toast.makeText(MainActivity.this, "Backup Done Succesfully!", Toast.LENGTH_LONG)
            .show();
            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
       }
}

}

当我运行上面的代码时,它会给我一些消息,如

01-11 09:52:35.296: W/System.err(966): java.io.FileNotFoundException: /mounted/data/app/com.example.gridwithcheckbox-1.apk (No such file or directory)
01-11 09:52:35.315: W/System.err(966):  at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-11 09:52:35.315: W/System.err(966):  at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
01-11 09:52:35.315: W/System.err(966):  at java.io.FileInputStream.<init>(FileInputStream.java:80)
01-11 09:52:35.315: W/System.err(966):  at java.io.FileInputStream.<init>(FileInputStream.java:132)
01-11 09:52:35.315: W/System.err(966):  at com.example.backupdemo.MainActivity.onCreate(MainActivity.java:46)
01-11 09:52:35.315: W/System.err(966):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-11 09:52:35.315: W/System.err(966):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-11 09:52:35.315: W/System.err(966):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-11 09:52:35.315: W/System.err(966):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-11 09:52:35.315: W/System.err(966):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-11 09:52:35.315: W/System.err(966):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 09:52:35.315: W/System.err(966):  at android.os.Looper.loop(Looper.java:123) 
01-11 09:52:35.315: W/System.err(966):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-11 09:52:35.315: W/System.err(966):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 09:52:35.315: W/System.err(966):  at java.lang.reflect.Method.invoke(Method.java:507)
01-11 09:52:35.315: W/System.err(966):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-11 09:52:35.315: W/System.err(966):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-11 09:52:35.315: W/System.err(966):  at dalvik.system.NativeStart.main(Native Method)

我还提到了清单文件的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

所以我知道如何解决它?

使用Tamilan Code我会像以下一样获取logcat

01-11 10:46:19.685: V/file--(14637):  com.example.tabpager-2.apk----TabPAger
01-11 10:46:19.685: D/file_name--(14637):  TabPAger
01-11 10:46:19.685: I/System.out(14637): No such file or directory
01-11 10:46:19.685: V/file--(14637):  it.anddev.tutorial-1.apk----KeyboardWidgetTutorial
01-11 10:46:19.695: D/file_name--(14637):  KeyboardWidgetTutorial
01-11 10:46:19.695: I/System.out(14637): No such file or directory
01-11 10:46:19.695: V/file--(14637):  com.example.crudopration-2.apk----CrudOpration
01-11 10:46:19.695: D/file_name--(14637):  CrudOpration
01-11 10:46:19.695: I/System.out(14637): No such file or directory
01-11 10:46:19.705: V/file--(14637):  net.leolink.android.simpleinfinitecarousel- 1.apk----SimpleInfiniteCarousel
01-11 10:46:19.705: D/file_name--(14637):  SimpleInfiniteCarousel

1 个答案:

答案 0 :(得分:16)

试试这个..

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
        int z = 0;
        for (Object object : pkgAppsList) {

            ResolveInfo info = (ResolveInfo) object;

            File f1 =new File( info.activityInfo.applicationInfo.publicSourceDir);

            Log.v("file--", " "+f1.getName().toString()+"----"+info.loadLabel(getPackageManager()));
                try{

                    String file_name = info.loadLabel(getPackageManager()).toString();
                    Log.d("file_name--", " "+file_name);

                   // File f2 = new File(Environment.getExternalStorageDirectory().toString()+"/Folder/"+file_name+".apk");
                   // f2.createNewFile();

                    File f2 = new File(Environment.getExternalStorageDirectory().toString()+"/Folder");   
                    f2.mkdirs();            
                    f2 = new File(f2.getPath()+"/"+file_name+".apk");
                    f2.createNewFile();

                    InputStream in = new FileInputStream(f1);

                    OutputStream out = new FileOutputStream(f2);

                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0){
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                    System.out.println("File copied.");
                }
                catch(FileNotFoundException ex){
                    System.out.println(ex.getMessage() + " in the specified directory.");
                }
                catch(IOException e){
                    System.out.println(e.getMessage());      
                }
        }