复制" .apk文件"文件到指定的文件夹 [英] Copying ".apk" file to specified folder

查看:201
本文介绍了复制" .apk文件"文件到指定的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表视图复制安装的应用程序的APK的apk到一些指定folder.Copying工作正常...但每次我点击应用程序来复制其APK ......phone.apk被复制到与差异包名的目标文件夹。
以下是文件复制的code。

I am trying to copy apk of installed app from listview to some specified folder.Copying of apk is working fine...But each time i click on app to copy its apk ..."phone.apk" gets copied to destination folder with diff package name. Here is code of file copying.

              public class MainActivity extends ListActivity {
PackageManager packageManager;
List<ApplicationInfo> applist;
Listadapter listadapter;

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

    packageManager = getPackageManager();

    new LoadApplications().execute();

}

@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Choose option")
            .setItems(R.array.options, new  DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {

                        case 0:
                            ApplicationInfo app3 = applist.get(position);
                            packageName=app3.packageName;
                            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 f1 = new File(info.activityInfo.applicationInfo.publicSourceDir);
                                try{                          
                                    File f2 = new File(Environment.getExternalStorageDirectory().toString()+"/Rahul");
                                    f2.mkdirs();
                                    f2 = new File(f2.getPath()+"/"+packageName + ".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();
                                    Toast.makeText(MainActivity.this,"Copied",Toast.LENGTH_LONG).show();
                                }
                                catch(FileNotFoundException ex){
                                    System.out.println(ex.getMessage() + " in the specified directory.");
                                }
                                catch(IOException e){
                                    System.out.println(e.getMessage());
                                }
                                break;
                            }
                    }
                }
            });
    dialogBuilder.setCancelable(true)
            .show();

}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    ArrayList<ApplicationInfo> applist = new ArrayList<>();

    for (ApplicationInfo info : list) {
        try {
            if (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
                applist.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return applist;
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;

    @Override
    protected Void doInBackground(Void... params) {
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));

        listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist);

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(aVoid);

    }

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,");
        super.onPreExecute();
    }

}
}

这是哪个文件被复制的目标文件夹的SS。

推荐答案

您能得到安装的应用程序环路软件包列表的列表中,但是没有比较你的包名称和包的这是在循环来的名字。所以最后的应用程序被保存。请使用这个希望它会帮助你。评论部分是增加了

you was able to get list of installed apps loop as package list but you was not comparing your package name and the name of package which was coming in the loop. So the last app was saved. Please use this hope it will help you. Commented portion is the addition

public class MainActivity extends ListActivity {
    PackageManager packageManager;
    List<ApplicationInfo> applist;
    Listadapter listadapter;

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

        packageManager = getPackageManager();

        new LoadApplications().execute();

    }

    @Override
    protected void onListItemClick(ListView l, View v, final int position,
            long id) {
        super.onListItemClick(l, v, position, id);

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Choose option").setItems(R.array.options,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {

                        case 0:
                            ApplicationInfo app3 = applist.get(position);

                            // this is the pakage name of tha app you clicked
                            packageName = app3.packageName;
                            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;
                                // check whther the app clicked and the app in
                                // the loop has same package name
                                if (info.activityInfo.packageName
                                        .equals(packageName)) {
                                    File f1 = new File(
                                            info.activityInfo.applicationInfo.publicSourceDir);
                                    try {
                                        File f2 = new File(Environment
                                                .getExternalStorageDirectory()
                                                .toString()
                                                + "/Rahul");
                                        f2.mkdirs();
                                        f2 = new File(f2.getPath() + "/"
                                                + packageName + ".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();
                                        Toast.makeText(MainActivity.this,
                                                "Copied", Toast.LENGTH_LONG)
                                                .show();
                                    } catch (FileNotFoundException ex) {
                                        System.out.println(ex.getMessage()
                                                + " in the specified directory.");
                                    } catch (IOException e) {
                                        System.out.println(e.getMessage());
                                    }
                                    break;
                                }
                            }
                        }
                    }
                });
        dialogBuilder.setCancelable(true).show();

    }

    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
  ArrayList<ApplicationInfo> applist = new ArrayList<>();

  for (ApplicationInfo info : list) {
      try {
          if (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
              applist.add(info);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  return applist;
}

    private class LoadApplications extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager
                    .getInstalledApplications(PackageManager.GET_META_DATA));

            listadapter = new Listadapter(MainActivity.this,
                    R.layout.list_item, applist);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            setListAdapter(listadapter);
            progress.dismiss();
            super.onPostExecute(aVoid);

        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(MainActivity.this, null,
                    "loading apps info,,,");
            super.onPreExecute();
        }

    }
}

这篇关于复制&QUOT; .apk文件&QUOT;文件到指定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆