本文介绍了在Android中以编程方式将所有安装的APK文件备份到sdcard中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在sdcard中备份所有安装的应用程序,因此我在下面的代码中编写

I want to take back up of my all install application in my sdcard so I write below code

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

}

当我运行上述代码时,它会给我类似

when i run above code it give me message like

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)

我还提到了清单文件的许可权

and i also mention permission to manifest file

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

所以我知道如何解决吗?

so any idea how can i solve it?

使用塔米伦代码,使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

推荐答案

尝试一下..

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

这篇关于在Android中以编程方式将所有安装的APK文件备份到sdcard中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 11:30