gpt4 book ai didi

java - Android,从任何 URL/任何文件、avi 等保存。它仅在模拟器上运行,不在 Samsung GalaxyS3 上运行

转载 作者:行者123 更新时间:2023-12-01 13:18:44 25 4
gpt4 key购买 nike

我想为Android开发一个简单的代码。它在模拟器上运行并下载了 *.wmv 文件,但它不在我的三星 Galaxy S3 上运行!!!

我可以解决这个问题吗?有人知道这个问题吗?

该软件应满足以下条件;

- Video file download from any URL (http://www.MySite.com/test/test.wmv)
- Save to SD Card or to system area/drive

开发工具:

eclipse 开普勒,

最小。要求。 SDK:API 8:Android 2.2。 (弗罗约)

目标 SDK:API 18

编译:API 19:Android 4.4.2

这是我的代码行;

AndroidManifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

MainActivity.java

package op.software.videodownloadtest1;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;


public class IlkMainActivity extends Activity {
private final int TIMEOUT_CONNECTION= 5000; // 5 sec.
private final int TIMEOUT_SOCKET=30000; // 30 sec.
private String filePathOP = "...";


public void DownloadFromURL(String videoURL, String fileName){
FileOutputStream outStream = null;
BufferedInputStream inStream = null;

try {
URL url = new URL(videoURL);

String storagePath = "/storage/extSdCard/Android/data/op.software.video/";
File file = new File(storagePath, fileName);
TextView tView = (TextView) findViewById(R.id.ResultText);
tView.setText("Download Path: " + file.getPath() + "---" + file.getName());
filePathOP = file.getPath() + "---" + file.getName();

long startTime = System.currentTimeMillis();
Log.i("VideoDownloaderTest1", "Download has started!");
Log.i("VideoDownloaderTest1", "Download url:" + url);
Log.i("VideoDownloaderTest1", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();

//this timeout affects how long it takes for the app to realize there's a connection problem
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);

//Define InputStreams to read from the URLConnection.
InputStream is = ucon.getInputStream();
inStream = new BufferedInputStream(is, 1024 * 5);
outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];

//Read bytes (and store them) until there is nothing more to read(-1)
int len;
while ((len = inStream.read(buff)) != -1)
{
outStream.write(buff,0,len);
}

Log.i("VideoDownloaderTest1", "download completed in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");

tView = (TextView) findViewById(R.id.ResultText);
tView.setText("Download başarılı :)" + filePathOP);

DialogShowMsg("Download succesfull", "File did download \r\n" + filePathOP);

} catch (IOException e) {
Log.i("VideoDownloaderTest1", "!!! Error !!!: " + e);
TextView tView = (TextView) findViewById(R.id.ResultText);
DialogShowMsg("Download Unsuccessful", "Error \r\n" + e.toString() +"\r\n" + filePathOP);
tView.setText(e.toString());
}
finally {
try{
//clean up
if (outStream != null) {
outStream.flush();
outStream.close();
}
if (inStream != null)
inStream.close();
} catch (IOException ioe){
Log.i("VideoDownloaderTest2", "!!! Error !!!: " + ioe);
TextView tView = (TextView) findViewById(R.id.ResultText);
tView.setText(ioe.toString());
}

}
}

private void DialogShowMsg(String alertTitle, String alertContent){
AlertDialog.Builder builder = new AlertDialog.Builder(IlkMainActivity.this);

builder.setMessage(alertContent)
.setTitle(alertTitle);

AlertDialog dialog = builder.create();

dialog.show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ilk_main);

Button btn = (Button) findViewById(R.id.HereDownload);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DownloadFromURL("http://www.mysite.com/test/test.wmv", "test.wmv");
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ilk_main, menu);
return true;
}

}

最佳答案

使用AsyncTask/ExecutorService/Thread进行URl连接 android.os.NetworkOnMainThreadException

关于java - Android,从任何 URL/任何文件、avi 等保存。它仅在模拟器上运行,不在 Samsung GalaxyS3 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com