采取一切安装apk文件备份到Android中programitically SD卡 [英] Take backup of all install apk file into sdcard programitically in android
本文介绍了采取一切安装apk文件备份到Android中programitically SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友你好我想拿回我的在我的SD卡安装的所有应用程序,所以我下面写code
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最终意图mainIntent =新的意图(Intent.ACTION_MAIN,NULL);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
。最终名单pkgAppsList = getPackageManager()queryIntentActivities(mainIntent,0);
对于(Object对象:pkgAppsList){
ResolveInfo信息=(ResolveInfo)对象;
档案文件=新的文件(info.activityInfo.applicationInfo.publicSourceDir);
InputStream的myInput;
的System.out.println(SDSD+ file.toString());
。串sdpath = Environment.getExternalStorageDirectory()getPath();
尝试 {
文件中存在=新的文件(Environment.getExternalStorageState()+ file.toString());
的System.out.println(1+ exist.exists());
//设置输出文件夹的SCARD
文件目录=新的文件(sdpath +/后退);
//创建文件夹,如果它不存在:
如果(!directory.exists()){
directory.mkdirs();
}
//设置输出文件流起来:
myInput =新的FileInputStream(Environment.getExternalStorageState()+ file.toString());
的OutputStream myOutput =新的FileOutputStream(directory.getPath()+ file.toString());
//将字节从输入文件到输出文件
byte []的缓冲区=新的字节[1024];
INT长;
而((长度= myInput.read(缓冲液))大于0){
myOutput.write(缓冲液,0,长度);
}
//关闭并清除流
myOutput.flush();
myOutput.close();
myInput.close();
Toast.makeText(MainActivity.this,备份完成成功与!,Toast.LENGTH_LONG)
。显示();
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
}
当我运行上面的code它给我留言像
九月一日至11号:52:35.296:W / System.err的(966):java.io.FileNotFoundException:/mounted/data/app/com.example.gridwithcheckbox-1 apk文件(没有这样的文件或目录)
9月一号至11日:52:35.315:W / System.err的(966):在org.apache.harmony.luni.platform.OSFileSystem.open(本机方法)
9月一号至11日:52:35.315:W / System.err的(966):在dalvik.system.BlockGuard $ WrappedFileSystem.open(BlockGuard.java:232)
9月一号至11日:52:35.315:W / System.err的(966):在java.io.FileInputStream中< INIT>(FileInputStream.java:80)
9月一号至11日:52:35.315:W / System.err的(966):在java.io.FileInputStream中< INIT>(FileInputStream.java:132)
9月一号至11日:52:35.315:W / System.err的(966):在com.example.backupdemo.MainActivity.onCreate(MainActivity.java:46)
9月一号至11日:52:35.315:W / System.err的(966):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
9月一号至11日:52:35.315:W / System.err的(966):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
9月一号至11日:52:35.315:W / System.err的(966):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
9月一号至11日:52:35.315:W / System.err的(966):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
9月一号至11日:52:35.315:W / System.err的(966):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
9月一号至11日:52:35.315:W / System.err的(966):在android.os.Handler.dispatchMessage(Handler.java:99)
9月一号至11日:52:35.315:W / System.err的(966):在android.os.Looper.loop(Looper.java:123)
9月一号至11日:52:35.315:W / System.err的(966):在android.app.ActivityThread.main(ActivityThread.java:3683)
9月一号至11日:52:35.315:W / System.err的(966):在java.lang.reflect.Method.invokeNative(本机方法)
9月一号至11日:52:35.315:W / System.err的(966):在java.lang.reflect.Method.invoke(Method.java:507)
9月一号至11日:52:35.315:W / System.err的(966):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
9月一号至11日:52:35.315:W / System.err的(966):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
9月一号至11日:52:35.315:W / System.err的(966):在dalvik.system.NativeStart.main(本机方法)
和我还提到权限清单文件
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
所以任何想法,我怎么能解决呢?
使用Tamilan codeI化妆得到类似的logcat以下
10月1日至11日:46:19.685:V /文件 - (14637):com.example.tabpager-2.apk ---- TabPAger
十月一号至11号:46:19.685:D / FILE_NAME - (14637):TabPAger
十月一号至11号:46:19.685:我/的System.out(14637):没有这样的文件或目录
十月一号至11号:46:19.685:V /文件 - (14637):it.anddev.tutorial-1.apk ---- KeyboardWidgetTutorial
十月一号至11号:46:19.695:D / FILE_NAME - (14637):KeyboardWidgetTutorial
十月一号至11号:46:19.695:我/的System.out(14637):没有这样的文件或目录
十月一号至11号:46:19.695:V /文件 - (14637):com.example.crudopration-2.apk ---- CrudOpration
十月一号至11号:46:19.695:D / FILE_NAME - (14637):CrudOpration
十月一号至11号:46:19.695:我/的System.out(14637):没有这样的文件或目录
十月一号至11号:46:19.705:V /文件 - (14637):net.leolink.android.simpleinfinitecarousel- 1.apk ---- SimpleInfiniteCarousel
十月一号至11号:46:19.705:D / FILE_NAME - (14637):SimpleInfiniteCarousel
解决方案
试试这个..
最终意图mainIntent =新的意图(Intent.ACTION_MAIN,NULL);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
。最终名单pkgAppsList = getPackageManager()queryIntentActivities(mainIntent,0);
INT Z = 0;
对于(Object对象:pkgAppsList){
ResolveInfo信息=(ResolveInfo)对象;
文件F1 =新的文件(info.activityInfo.applicationInfo.publicSourceDir);
Log.v(file--,+ f1.getName()的toString()+。----+ info.loadLabel(getPackageManager()));
尝试{
串FILE_NAME = info.loadLabel(getPackageManager())的toString()。
Log.d(file_name--,+ file_name中);
//文件F2 =新的文件(Environment.getExternalStorageDirectory()的toString()+/文件夹/+ FILE_NAME +。APK);
// f2.createNewFile();
文件F2 =新的文件(Environment.getExternalStorageDirectory()的toString()+/文件夹。);
f2.mkdirs();
F2 =新的文件(f2.getPath()+/+ FILE_NAME +APK);
f2.createNewFile();
InputStream的时间=新的FileInputStream(F1);
的OutputStream OUT =新的FileOutputStream(F2);
byte []的BUF =新的字节[1024];
INT LEN;
而((LEN = in.read(BUF))大于0){
out.write(BUF,0,的len);
}
附寄();
out.close();
的System.out.println(文件复制的。);
}
赶上(FileNotFoundException异常前){
的System.out.println(ex.getMessage()+在指定的目录。);
}
赶上(IOException异常E){
的System.out.println(e.getMessage());
}
}
Hello friends 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?
Using Tamilan Code i make get logcat like following
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
解决方案
Try this..
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());
}
}
这篇关于采取一切安装apk文件备份到Android中programitically SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文