我的Android应用程序允许用户从Internet下载zip文件。有人可以告诉您是否可以从应用程序本身安装zip文件吗?无法访问互联网。我一直在寻找机会这样做,但已经茫然了。
private void gotoact1(View view)
{
switch (view.getId()) {
case R.id.buttonDow:
Intent intent3 = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://drive.google.com/file/d/16eu-
2gI8c549rt6Ve04UIyDm3oeGAHeu/view?usp=sharing"));
startActivity(intent3);
break;
default:
break;
}
}
public void onClick(View view) {
//requestNewInterstitial();
clicked = view;
{gotoact1(clicked); }
}
P.S。感谢您的答复,我开始理解! 我将zip文件添加到名称为“ my.zip”的“ raw”文件夹中,并添加了代码(如下所示),不幸的是没有在手机上安装该文件,您能看到我的错误吗?
private void gotoact1(View view)
{
switch (view.getId()) {
case
R.id.buttonDowN:
saveResourceToFile();
break;
default:
break;
}
}
public void saveResourceToFile() {
InputStream in = null;
FileOutputStream fout = null;
try {
in = getResources().openRawResource(R.raw.signature);
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filename = "my.zip";
fout = new FileOutputStream(new File(downloadsDirectoryPath + "/" + filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
Toast toast = Toast.makeText(getApplicationContext(),
"DownloadOne!", Toast.LENGTH_SHORT);
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),
"DownloadTwo!", Toast.LENGTH_SHORT);
toast.show();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我在代码中添加了Toast,并包含了DownloadTwo。为什么打开FileNotFoundException?我还是不明白:( 很不好意思我自己也无法弄清楚,对不起
2019-03-24 05:10:03.662 5385-5385 / com.example.myapplication W / System.err:java.io.FileNotFoundException:/storage/emulated/0/Download/my.zip(权限被拒绝) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:at java.io.FileOutputStream.open0(本机方法) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:at java.io.FileOutputStream.open(FileOutputStream.java:308) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:at java.io.FileOutputStream。(FileOutputStream.java:238) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:at java.io.FileOutputStream。(FileOutputStream.java:180) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在com.example.myapplication.Main4.saveResourceToFile(Main4.java:57) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在com.example.myapplication.Main4.gotoact1(Main4.java:42) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在com.example.myapplication.Main4.onClick(Main4.java:33) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在android.view.View.performClick(View.java:6648) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在android.view.View.performClickInternal(View.java:6620) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在android.view.View.access $ 3100(View.java:787) 2019-03-24 05:10:03.663 5385-5385 / com.example.myapplication W / System.err:在android.view.View $ PerformClick.run(View.java:26167) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:位于android.os.Handler.handleCallback(Handler.java:891) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:在android.os.Handler.dispatchMessage(Handler.java:102) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:位于android.os.Looper.loop(Looper.java:207) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:在android.app.ActivityThread.main(ActivityThread.java:7536) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:at java.lang.reflect.Method.invoke(本机方法) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:at com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:524) 2019-03-24 05:10:03.664 5385-5385 / com.example.myapplication W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958) 2019-03-24 05:10:03.690 5385-5385 / com.example.myapplication D / HwAppInnerBoostImpl:asyncReportData com.example.myapplication,2,1,1,0 interval = 80 2019-03-24 05:10:03.706 5385-5412 / com.example.myapplication D / OpenGLRenderer:启用HWUI Binary 2019-03-24 05:10:03.740 5385-5412 / com.example.myapplication D / mali_winsys:EGLint new_window_surface(egl_winsys_display *,void *,EGLSurface,EGLConfig,egl_winsys_surface **,EGLBoolean)返回0x3000 2019-03-24 05:10:03.741 5385-5412 / com.example.myapplication D / OpenGLRenderer:启用HWUI Binary 2019-03-24 05:10:03.940 5385-5385 / com.example.myapplication D / HwAppInnerBoostImpl:asyncReportData com.example.myapplication,2,1,2,0间隔= 330 2019-03-24 05:10:05.332 5385-5385 / com.example.myapplication D / AwareBitmapCacher:handleInit开关未打开pid = 5385 2019-03-24 05:10:05.696 5385-5412 / com.example.myapplication W / libEGL:EGLNativeWindowType 0x7abd817010断开失败
答案 0 :(得分:0)
将zip文件从您应用的原始文件夹复制到Android下载文件夹是否可以解决?
我认为this question很相似。
但是,您将需要添加权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
就我而言,我还必须修复答案的以下部分。
添加:+ "/"
fout = new FileOutputStream(new File(downloadsDirectoryPath + "/" + filename));