如何打开assets文件夹中的apk?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何打开assets文件夹中的apk?相关的知识,希望对你有一定的参考价值。
如题,小弟在assets文件夹中有个a.apk文件,我该如何做才能在程序中安装这个apk呢,请大侠们明示,小弟谢过先!
在自己的app中安装assets目录下的apk文件的方法:
详细过程如下:
public class MainActivity extends Activity
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
//Toast.makeText(this, ""+Environment.getExternalStorageDirectory().getAbsolutePath(), 0).show();
if(copyApkFromAssets(this, "test.apk", Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.apk"))
Builder m = new AlertDialog.Builder(mContext)
.setIcon(R.drawable.ic_launcher).setMessage("是否安装?")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("yes", new OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.apk"),
"application/vnd.android.package-archive");
mContext.startActivity(intent);
);
m.show();
public boolean copyApkFromAssets(Context context, String fileName, String path)
boolean copyIsFinish = false;
try
InputStream is = context.getAssets().open(fileName);
File file = new File(path);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0)
fos.write(temp, 0, i);
fos.close();
is.close();
copyIsFinish = true;
catch (IOException e)
e.printStackTrace();
return copyIsFinish;
参考技术A 通常的做法是将assets下面的apk拷贝到手机内存或者sdcard中,然后进行安装 参考技术B 1、安装APK代码 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive"); mContext.startActivity(intent);2、拷贝APK代码:/** * 将assets文件夹下的apk fileName,拷贝到路径path * * @param context * 上下文环境 * @param fileName * apk名称 * @param path * 存储APK路径 * @return */ public boolean copyApkFromAssets(Context context, String fileName, String path) boolean bRet = false; try InputStream is = context.getAssets().open(fileName); File file = new File(path); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); byte[] temp = new byte[1024]; int i = 0; while ((i = is.read(temp)) > 0) fos.write(temp, 0, i); fos.close(); is.close(); bRet = true; catch (IOException e) e.printStackTrace(); return bRet; 参考技术C 安装这样: http://www.devdiv.com/viewthread.php?tid=38533&highlight=apk路径获取activity().getAssets() 参考技术D 下面通过一个例子讲解读取资源文件显示在ScrollView当中:
1.ReadAsset.java文件:
Java代码
package com.example.ReadAsset;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
public class ReadAsset extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.read_asset);
try
//Return an AssetManager instance for your application's package
InputStream is = getAssets().open("index.txt");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer, "GB2312");
// Finally stick the string into the text view.
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(text);
catch (IOException e)
// Should never happen!
throw new RuntimeException(e);
2. read_asset.xml文件
Java代码
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:paddingTop="50dip">
android:layout_height="wrap_content" android:textStyle="normal" />
3.然后在工程里面新建一个assets文件夹,随便放一个index.txt的文件在其中,运行
Ctrl+F11进行测试即可。
为啥apk文件用apktool反编译后修改assets文件中的内容不起作用啊?
怎样才能使反编译的文件中修改了内容还能编译显现?
参考技术A 反编译後要编译,还要签名啊!建议用 ApkIDE改之理, 用它修改APK比较容易, 因为它综合了修改APK必要的工具
加油,希望帮到你 ^_^
以上是关于如何打开assets文件夹中的apk?的主要内容,如果未能解决你的问题,请参考以下文章