Java - file reading function

This code using java.util.Scanner class to read file to String, and very helpful for Java Application developing.

private static String readFile(String pathname) throws IOException {
   File file = new File(pathname);
   StringBuilder fileContents = new StringBuilder((int)file.length());
   Scanner scanner = new Scanner(file);
   String lineSeparator = System.getProperty("line.separator");
   try {
       while(scanner.hasNextLine()) {       
           fileContents.append(scanner.nextLine() + lineSeparator);
       }
       return fileContents.toString();
   } finally {
       scanner.close();
   }
}

Android WebView can not load HTTPS

When developing some Android apps, I have a problem with WebView. Some HTTPS urls aren't working, it say "This page can not displayed", but they are working well in browser. The problem is Certificate of SSL, when your WebView can't authorize the website. So, I have a simple solution to ignore this error: catch the event "onReceivedSslError" of WebViewClient and set WebViewClient object in WebView:

public void onCreate(Bundle savedInstance)
{       
    super.onCreate(savedInstance);
    setContentView(R.layout.your_layout);

    webView=(WebView)findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new DialogWebViewClient());
    String url ="https://yoursite.com";
    webView.loadUrl(url);
    
}

private class DialogWebViewClient extends WebViewClient {
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
}

Change the android media volume programmatically

The code below can be used to change media volume, when your application or your device playing sounds.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
 switch (keyCode) {
     case KeyEvent.KEYCODE_VOLUME_UP:
         audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                 AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
         return true;
     case KeyEvent.KEYCODE_VOLUME_DOWN:
         audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                 AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
         return true;
     default:
         return super.onKeyDown(keyCode, event);
 }
}

Android Emulator is too slow - solutions?

I 'm very mad with the Emulator because it 's very very slow. And I 'm using a laptop which have no strong hardwares. It took me more than 1 minute to run an application. Some of solutions to make the Emulator faster is ineffective for me. So, i searched some solutions without using the default emulator of android-sdk. And now i'm very happy with a new Android Virtual Machine (AVM).

Solution?
- Install Android OS in some virtual machine software (VMWare, VirtualBox, ...). Note that your Android has a root permission to open the connection by "adb connect" via port 5555.
- Check the AVM IPAddress, and open cmd (command prompt) in android-sdk/platform-tool folder:
> adb connect 192.168.111.100 (your AVM IP)
If success, you can check devices by command:
> adb devices
- And use your IDE to build application with the target is a new AVM.

Tip: You can download the AVM (VirtualBox) here: androvm.org/blog/download/

Why using AVM?
- It run faster than the Emulator, of course.
- Make snapshot or suspend AVM, decrease time when starting.
- Easy change the resolution or density of device.
- Easy change the size of screen of AVM by mouse.
etc...


Tip: Play sound from internal storage

Why play sound from internal storage?
Ordinary we play sounds (songs, ring tunes, ...) from mobile 's sdcard. But in some case, we have a sound file in internal storage(ex: default ring tunes of devices, downloaded file, device does not have a sdcard, ...), and we need to play it without copying to sdcard.

And the code:

FileInputStream fileInputStream = new FileInputStream(path);
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(fileInputStream.getFD());
mp.prepare();
Note:
- Close fileInputStream when mediaPlayer completed.
- Don't forget to add a permission READ_INTERNAL_STORAGE to Manifest:
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

P/s: Before publishing this post, I developed an application for English Learning. It has some Listening Practices. There 're some problems when streaming file from server. So i created a downloading service to download all sound files to internal storage and play them. So, this code is helpful for me.

Powered by Blogger.