Android 复制;。apk";将文件复制到指定文件夹
我正在尝试将已安装应用程序的apk从listview复制到某个指定文件夹。apk的复制工作正常…但每次我单击应用程序以复制其apk…“phone.apk”将被复制到具有不同软件包名称的目标文件夹。 这是文件复制的代码Android 复制;。apk";将文件复制到指定文件夹,android,image,file,android-listview,inputstream,Android,Image,File,Android Listview,Inputstream,我正在尝试将已安装应用程序的apk从listview复制到某个指定文件夹。apk的复制工作正常…但每次我单击应用程序以复制其apk…“phone.apk”将被复制到具有不同软件包名称的目标文件夹。 这是文件复制的代码 public class MainActivity extends ListActivity { PackageManager packageManager; List<ApplicationInfo> applist; Listadapte
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();
}
}
}
public类MainActivity扩展了ListActivity{
包装管理器包装管理器;
List您可以将已安装的应用程序列表循环为程序包列表,但您没有将程序包名称与循环中的程序包名称进行比较。因此最后一个应用程序已保存。请使用此选项,希望它能帮助您。注释部分为添加内容
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();
}
}
}
public类MainActivity扩展了ListActivity{
包装管理器包装管理器;
列表应用程序列表;
列表适配器列表适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager=getPackageManager();
新建LoadApplications().execute();
}
@凌驾
仅受保护的无效列表单击(列表视图l、视图v、最终整数位置、,
长id){
super.onListItemClick(左、右、位置、id);
AlertDialog.Builder dialogBuilder=新建AlertDialog.Builder(此);
dialogBuilder.setTitle(“选择选项”).setItems(R.array.options,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
开关(哪个){
案例0:
ApplicationInfo app3=applist.get(位置);
//这是您单击的tha应用程序的包装名称
packageName=app3.packageName;
最终意图maintent=新意图(
Intent.ACTION_MAIN,空);
mainIntent.addCategory(Intent.CATEGORY_启动器);
最终列表pkgAppsList=getPackageManager()
.查询活动(mainIntent,0);
for(对象:pkgAppsList){
ResolveInfo=(ResolveInfo)对象;
//检查应用程序单击的位置和应用程序的位置
//循环具有相同的包名
如果(info.activityInfo.packageName
.equals(packageName)){
文件f1=新文件(
info.activityInfo.applicationInfo.publicSourceDir);
试一试{
文件f2=新文件(环境)
.getExternalStorageDirectory()
.toString()
+“/Rahul”);
f2.mkdirs();
f2=新文件(f2.getPath()+“/”
+packageName+“.apk”);
f2.createNewFile();
InputStream in=新文件InputStream(f1);
OutputStream out=新文件OutputStream(
f2);
字节[]buf=新字节[1024];
内伦;
而((len=in.read(buf))>0){
out.write(buf,0,len);
}
in.close();
out.close();
Toast.makeText(MainActivity.this,
“复制”,吐司。长度(长)
.show();
}捕获(FileNotFoundException ex){
System.out.println(例如getMessage()
+“在指定目录中。”);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
打破
}
}
}
}
});
dialogBuilder.setCancelable(true.show();
}
私人列表checkForLaunchIntent(列表列表){
ArrayList applist=新的ArrayList();
用于(应用程序信息:列表){
试一试{
if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
添加(信息);
}
}捕获(例外e){
e、 printStackTrace();
}
}
返回应用程序列表;
}
私有类装入应用程序扩展异步任务{
private ProgressDialog progress=null;
@凌驾
受保护的Void doInBackground(Void…参数){
应用程序列表=checkForLaunchIntent(packageManager
.GetInstalledApplication(PackageManager.GET_META_DATA));
listadapter=新listadapter(MainActivity.this,
R.layout.list_项,应用列表);
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
setListAdapter(listadapter);
进步。解散();
super.onPostExecute(避免);
}
@凌驾
受保护的void onPreExecute(){
progress=ProgressDialog.show(MainActivity.this,null,
“正在加载应用程序信息,,”;