Java 下载音频Zip文件并将其保存在内部存储器中,然后使用媒体播放器播放
代码 请指导我如何存储(音频)文件,以便我可以在媒体播放器上使用它们作为播放列表,或者告诉我,如果我做错了什么,我以前没有使用过文件,因此任何简明和基本的解释都将不胜感激。提前谢谢 ps,我能够读取logcat中的文件名,这意味着我正在获取或下载它们Java 下载音频Zip文件并将其保存在内部存储器中,然后使用媒体播放器播放,java,android,Java,Android,代码 请指导我如何存储(音频)文件,以便我可以在媒体播放器上使用它们作为播放列表,或者告诉我,如果我做错了什么,我以前没有使用过文件,因此任何简明和基本的解释都将不胜感激。提前谢谢 ps,我能够读取logcat中的文件名,这意味着我正在获取或下载它们 @RequiresApi(api = Build.VERSION_CODES.M) @Override protected Void doInBackground(String... strings) {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected Void doInBackground(String... strings) {
try {
// mediaPlayer.setDataSource("http://www.archive.org/download/i_do_not_love_thee_norton_librivox/i_do_not_love_thee_norton_librivox_64kb_mp3.zip");
// mediaPlayer.prepare();
// duration = mediaPlayer.getDuration();
//TODO: CONTINUE FROM HERE (THE DATA HAS BEEN DOWNLOADED IN THE BACKGROUND THREAD)
//InputStream fis = new URL("http://www.archive.org/download/i_do_not_love_thee_norton_librivox/i_do_not_love_thee_norton_librivox_64kb_mp3.zip").openConnection().getInputStream();
URL url = new URL("http://www.archive.org/download/sonnets_portugese_knf_librivox/sonnets_portugese_knf_librivox_64kb_mp3.zip");
URLConnection urlConnection = url.openConnection();
//URL url = new URL("http://www.archive.org/download/tristan_iseult_librivox/tristan_iseult_librivox_64kb_mp3.zip/");
//URLConnection connection = url.openConnection();
ZipInputStream zis = new ZipInputStream(urlConnection.getInputStream());
ZipEntry ze;
//TODO: SETTING UP PROGRESS
//
int count = 0;
while ((ze = zis.getNextEntry()) != null) {
count++;
/**
* FILES ARE NOT IN ORDER AT THE TIME OF DOWNLOADING
*/
//TODO: HOW TO SAVE EACH ZIPENTRY (AUDIO FILE) IN ORDER TO PLAY THEM WITH MEDIAPLAYER?
name += count + ze.getName() + "\n";
//publishProgress(""+(int)((total*100)/lenghtOfFile));
Log.d("This", "doInBackground: " + ze.getName() + "\n");// + " Size: " + ze.getSize() + " Time: " + ze.getTime()
//zis.closeEntry();
}
zis.close();
} catch (IOException e) {
Log.d("This", "doInBackground: " + e.getMessage());
}
return null;
}
您正在尝试下载并解压缩该文件。有两种方法
public class DownloadVideoAsyncTask extends AsyncTask<String, Integer, String> {
private Context mContext;
public DownloadVideoAsyncTask(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
//output = new FileOutputStream("/data/data/com.example.vadym.test1/textfile.txt");
output = new FileOutputStream(mContext.getFilesDir() + "/file.mp4");
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.d("ptg", "onProgressUpdate: " + values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
这样您就可以解压缩文件了。我认为这个源代码的输出将在主文件上重新写入
编辑:
如何下载
File locationImage = new File(Environment.getExternalStorageDirectory()
+ "/Socio-Book/pictures/"+name+".png");
//save image to internal storage
direct = new File(Environment.getExternalStorageDirectory()
+ "/Socio-Book/pictures");
if (!direct.exists()) {
direct.mkdirs();
}
//save data to sqlite DB
boolean check=mydb.insertData(id,name,pass);
//download image or not
if(!locationImage.exists()) {
DownloadManager mgr = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(Constant.BASE_URL+"/y_chat/sign_up/"+name+"/profile.png");
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Image")
.setDestinationInExternalPublicDir("/Socio-Book/pictures", name + ".png");
mgr.enqueue(request);
下面的源代码需要进一步澄清
Uri downloadUri = Uri.parse(Constant.BASE_URL+"/y_chat/sign_up/"+name+"/profile.png");
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
它来自我上面的源代码。
Uri
是文件的位置(链接)。在这里,我是为常量写的。基本URL是网站链接。其他是文件的位置
Uri downloadUri = Uri.parse("http://www.archive.org/download/sonnets_portugese_knf_librivox/sonnets_portugese_knf_librivox_64kb_mp3.zip");
可能对你有用。首先检查您是否成功下载了这些文件。我相信这些文件正在下载并分配给ze(zipentry),但我不明白的是,如何在while循环中从zipentry获取每个文件的数据。对不起!在我运行1之后,无需澄清。代码,在我的设备上哪里可以找到下载文件?我的意思是我不明白这句话,mContext.getFilesDir(),是的,我现在明白了,我应该下载整个zip文件,然后从我的手机上解压所有这些文件,对吗?@KhanZalmi是的!我想你会在
下载文件中找到它。我不确定。在我编辑的源代码中,您可以在任何需要设置环境的特定位置获得它。getExternalStorageDirectory()+“/social Book/pictures/“+name+”.png”
对不起!它用于png
文件。所以,删除它并设置为您想要的,或者,设置您从该链接获得的任何内容。还有一件事,它是否会下载zip文件?因为我正在尝试下载zip文件,而不是任何其他文件和环境。getExternalStorageDirectory()=sd卡中的哪个文件夹?谢谢,我想我需要学习更多关于zip类的知识,以便能够正确地使用它们,因为现在我觉得我被stackoverflow上的许多不同代码淹没了。你尽力帮忙了,谢谢你。
Uri downloadUri = Uri.parse("http://www.archive.org/download/sonnets_portugese_knf_librivox/sonnets_portugese_knf_librivox_64kb_mp3.zip");