Take backup of all install apk file into sdcard programmatically in Android(在Android中以编程方式将所有安装apk文件备份到sdcard中)
本文介绍了在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?
使用我制作的 Tamilan 代码获取 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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在Android中以编程方式将所有安装apk文件备份到sdcard中
基础教程推荐
猜你喜欢
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01