【问题标题】:Copying ".apk" file to specified folder将“.apk”文件复制到指定文件夹
【发布时间】:2016-03-15 15:49:09
【问题描述】:

我正在尝试将已安装应用程序的 apk 从 listview 复制到某个指定文件夹。apk 的复制工作正常...但是每次我单击应用程序复制其 apk ...“phone.apk”被复制到具有差异包名称的目标文件夹。 这是文件复制的代码。

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

}
}

This is the ss of the destination folder in which file gets copied.

【问题讨论】:

    标签: android image file android-listview inputstream


    【解决方案1】:

    您能够获取已安装应用程序循环列表作为包列表,但您没有比较您的包名称和循环中的包名称。所以最后一个应用程序被保存了。请使用这个希望它会帮助你。注释部分是补充

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

    【讨论】:

    • 正如您在图像中看到的,包名称在目标文件夹中似乎是正确的,但在安装时,contacts.apk 被安装...在 easch 和每个人上
    • 如果你需要我可以发布完整的,如果数据有帮助
    • 再次感谢兄弟,它就像我想要的那样工作,你让代码理解的方式是 gud :]
    • vishal 这个问题我还需要多一点帮助...stackoverflow.com/questions/34193080/…
    • 很高兴看到它有帮助
    猜你喜欢
    • 1970-01-01
    • 2018-05-17
    • 2017-12-08
    • 2016-03-03
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    相关资源
    最近更新 更多