问题
I am trying to store audio files binary string into sdcard using android webview. But when I try to play an audio file in my mobile filemanager, the audio file is not playing. I am using JavaScript Interface for getting and saving audio file binary data into sdcard. I don't want to use an android download manager or another download manager for downloading files. I need to fix the below-posted code for audio play.
Here is code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
private WebView MCwv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String urlText ="http://susbd.com/bd.php?url=http://banglasongs.fusionbd.com/downloads/download.php?file=mp3/bangla/Romantic_Hitz/Tip_Tip_Brishti-Konika.mp3";
mProgressDialog = ProgressDialog.show(this, "", "Getting String...");
MCwv = (WebView) findViewById(R.id.mcwv);
WebSettings settings = MCwv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setUserAgentString("Opera/9.80 (Android; Opera Mini/36.2.2254/119.132; U; id) Presto/2.12.423 Version/12.16)");
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAllowFileAccess(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLoadsImagesAutomatically(true);
settings.setDefaultTextEncodingName("utf-8");
settings.setDomStorageEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
MCwv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
MCwv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
MCwv.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementsByTagName('html')[0].innerHTML);");
mProgressDialog.dismiss();
}
});
MCwv.loadUrl(urlText);
}
public class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void showHTML(String OutPut)
{
try {
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/mydl");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, "This_my_test_file.mp3");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream save = new FileOutputStream(file);
save.write(Html.fromHtml(OutPut).toString().substring(6).getBytes()); //html decoded and removed 6 string <html>
save.flush();
save.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
来源:https://stackoverflow.com/questions/61415330/stored-audio-files-using-android-webview-is-not-working