Showing posts with label Android SDK. Show all posts
Showing posts with label Android SDK. Show all posts

Monday, September 7, 2020

Connect to Web Socket From Your Android App using OkHttp

Hello,

In this blog I am going to explain how you can connect with Web Socket from your Android App using OkHttp. This will be useful when you want to send some data real time from your Android app to Web app. 

First add following line in your build.gradle file dependencies. 

implementation 'com.squareup.okhttp3:okhttp:3.11.0'

Now let the gradle build and sync. Next we will make a class which extends WebSocketListener from OkHttp3 and a new class and name it MyWebSocketListener.java and add following code to it. 

import okhttp3.Response;

import okhttp3.WebSocket;

import okhttp3.WebSocketListener;

import okio.ByteString;

public final class MyWebSocketListener extends WebSocketListener {

    private static final int NORMAL_CLOSURE_STATUS = 5000;

    public MyWebSocketListener() {  

    }

    @Override

    public void onOpen(WebSocket webSocket, Response response) {

        webSocket.close(NORMAL_CLOSURE_STATUS, "Exit");

    }

    @Override

    public void onMessage(WebSocket webSocket, String text) {

        //String Meesage received

    }

    @Override

    public void onMessage(WebSocket webSocket, ByteString bytes) {

        //Meesage received in form of binary data

    }

    @Override

    public void onClosing(WebSocket webSocket, int code, String reason) {

        webSocket.close(NORMAL_CLOSURE_STATUS, null);

        //Closing socket

    }

    @Override

    public void onFailure(WebSocket webSocket, Throwable t, Response response) {

        t.printStackTrace();

    }

}

Now we have listener created. Let's create client and attach it to Web Socket and Listener.

val listener = MyWebSocketListener()

val webSocket =   OkHttpClient.Builder().build().newWebSocket(request, listener)


That's it, now you can use this instance to send or receive message.

Tuesday, August 4, 2020

Android Build Error - The Crashlytics build ID is missing

Hello,

Recently I faced a very strange issue in one of my android app. We were using Fabric IO for crash analytics earlier in the app and since now it's already merged with Firebase Analytics. we need to update the app. 

I removed Firebase maven repository and removed all plugins from build gradle file and removed Fabric code from everywhere. And build was successful. After that I followed all the instructions on getting started page of Firebase and crash analytics and build was successful.  And then problem started. Once we run the app, it crashes and shows following error in Logcat. 

"The Crashlytics build ID is missing. This occurs when Crashlytics tooling is absent from your app's build configuration. Please review Crashlytics onboarding instructions and ensure you have a valid Crashlytics account

Now it seems there is an issue with Firebase crash analytics account so I regenerated the google service json file add updated in project. But it didn't help. 

Searched on Google, answers suggest to put back Fabric plugin again in gradle file. Which does not make any sense to me. Since Fabric is out dated, why do we need to add it again. 

Finally after 2 days I was able to solve the problem. Here is the solution

Remove google service json file. Use Firebase assistant and connect with your account again. Select the project and it again generate google service json file. Keep link to fabric io repository in top level gradle file.

maven { url 'https://maven.fabric.io/public' }

I don't know how it worked but it works and I really don't know the reason of why it didn't work and why it started working after above solution. But it does solve the problem. 

If anyone who is reading this blog, knows the reason please put in comment. 

Sunday, March 22, 2020

Socket.io Not Working on Android 9 (API 28)

Hi,

Recently in one of the project we faced a situation where we have used socket.io on backend and Android app connects to it. Socket.io was using http protocol for testing. It worked fine in older android versions but faced an issue in Android 9 and above where socket was connected but emit was not getting on server.

After couple of hours struggle finally found the problem. The problem was socket io was using http protocol and it's clearText HTTP request and it is banned in recent android version considering security reasons.

In earlier android version by default clearText is set as true but in later version default value is false. That means android components like HTTP and FTP and classes DownloadManager, and MediaPlayer can not use HTTP.

To over come this issue and allow socket to connect with HTTP protocol you have to allow clearText traffic on app level and for that you have to add following attribute in Application tag of your AndroidManifest.xml

<application
        android:usesCleartextTraffic="true">
.....
</application>

Once you add this to your manifest file, your socket emit will start working and you can still use HTTP with your socket.

Please note that since it was testing app we used HTTP based socket. However in production you should always use HTTPs protocol.

Hope this helps you.


Saturday, December 16, 2017

Android RecyclerView Add Load More Functionality

Hello,

Recently I was working on adding load more functionality on Android RecyclerView. The purpose was to load more data as soon as user scrolls to bottom and there are no records left.

Since we already have used swipe to refresh plugin other load more plugin was not working as the event was not attached and fired. If we remove swipe to remove then it worked but we needed both functionalities. So for that we did simple trick. Here is the code.

testRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if (!recyclerView.canScrollVertically(1)) {
            Toast.makeText(getApplicationContext(),"CALL LOAD MORE FUNCTION TO LOAD MORE DATA",Toast.LENGTH_LONG).show();
        }
    }
});

So trick is very simple, we added on scroll listener to recycler view and just checked it can not scroll any more vertically that means we reached at bottom and form here we can add logic to load more data.

Hope this helps you.

Saturday, February 11, 2017

Effective Way to Create and Save File In Android Application

Hello, in this blog post I am going to explain effective way of creating and saving file in android application. Also will explain what are the general mistakes developer make in creating and saving file and how to prevent it.

Why it is important to have strategy for File create and Save in Android?



Because android file system is very similar to disk based file system and when there is a problem in creating or saving file it throws IOException so your application may crash while it's running and it's not good for professional or business application. So you need a proper strategy to create and save file and you have to add all kind of checks and exception handling for this.


Common mistake by developers on Creating and Save file in Android


Any new developer who is working first time on the file make this mistake. They don't know how to create a file and they do not refer to android developer site and android SDK document. They just search on web and find answers on site like StackOverFlow and copy paste the code. But sometime what happens is the code which they have used was about saving file in SD card and it may be possible that some phones do not have SD card so when the application is running in these phones, it crashes.

Another mistake is, in the code they hard code the path of saving file. Each android version and phone has different way of handling SD card so if you hard code the path it may work in your phone but it does not work in other phone. So here you need some strategy for this and make sure that your code works in all scenarios.  Here are some common things you should consider while creating and saving file.


  1. Decide where you want to store file, in external storage or internal storage.
  2. If storing in internal storage make sure there is space available.
  3. If storing in external storage, first check if there is external storage is available and there is a space.
  4. Make sure you have permission to store files in external storage. 

If you have requirement that file created should only be accessed by your app, you should create it in internal storage in app directory or else you should create it in external storage. Here is the code I have used in one of my app to create file in external storage.

For this you should also have permission mentioned in your android manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
 Environment.DIRECTORY_PICTURES), "YOURAPP");

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("YOURAPP", "failed to create directory");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".3gp");
} else {
return null;
}
return mediaFile;
}

As you see in above code, we are first checking if external storage is available or not. If it's not available, you just return null and if it's available, depending on file you want to create, create unique file name with current time stamp. Do not use hard coded file name like myVideo.3gp and it will always overwrite the previously created file.

Now we will use above function to check if we have file created or not in external storage and if not created we will create it in internal storage.

try{
File outputFile = getOutputMediaFile(MEDIA_TYPE_VIDEO);

if(outputFile == null){
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
outputFile = new File(getFilesDir() + "/"  + File.separator +
"VID_"+ timeStamp + ".3gp");
}
} catch (IOException e) {
Log.d(TAG, "IOException creating file : " + e.getMessage());
}

As you see in above code we are calling this function to create file of type video and check if it returns null. If it returns null then we are creating file in internal storage. Following line creates file in internal directory.

outputFile = new File(getFilesDir() + "/"  + File.separator +
"VID_"+ timeStamp + ".3gp");

And we have enclosed code try catch block to catch any possible IOException. In case of exception, you should properly display a message to user.

Friday, February 3, 2017

Android SDK Content Loader failing with NullPointerException

I have just recently started getting an error any time I interact with the Android SDK and AVD Manager in Eclipse. It does not load SDK manager and when I try to open AVD manager it says please set "Android SDK path in preference". This was strange error I started getting since I added few AVDs with Google API and when I check log of NullPointerException it was showing error in AVD Info class so I was sure that error is related to AVD. May be I misconfigured AVD while creating and now it's not allowing me to do any work in Eclipse.

So to solve this error you have to delete .android directory located in your user profile folder if you are using Windows and if you are using Mac, this will be hidden directory in your home directory.

So first of all make all the hidden directory visible. First of all open terminal and type following commands.

defaults write com.apple.Fider AppleShowallFiles YES

Now you have to kill all the finders and then go to home directory.

Killall Finder

Now when you go to home directory you will see .android directory which was hidden.


Delete that directory and even delete it from trash.

Now restart the Eclipse and try to load AVD manager and Android SDK manager and it should work fine.

NOTE : Please note if you delete .android directory, you will loose all the AVD you have created before and some of your preference of eclipse, so please use it carefully. 

Monday, January 2, 2017

Android MediaRecorder Stop Failed - 1007

If you have ever used Android MediaRecorder to record video or audio in your android application, you must have faced this error. Recently I had nightmare in using MediaRecorder in my android application. I think it's most unstable class in Android SDK and there is a different behavior in each device. In this blog I am going to explain more about this and possible resolution of this.


First lets see wha's the meaning of this error : Android MediaRecorder Stop Failed - 1007

There is no documentation on Android developer site about this error code but from my experience I found out that there are three reasons behind this error

1) You called stop method too early.

According to documentation on MediaRecorder.java class it says

Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed. Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

That means if you start recording and in couple of seconds you call stop recording there is no significant data of recording hence stop method throws an exception. So you must wait for sometime. From my testing I found out that you should at least for minimum 10 seconds. So the solution is to have stop method called inside try catch block and handle the exception properly or do not allow user to stop recording till 10 seconds of start of recording. You can either disable stop button and make it enable after sometime. Once the exception is thrown, you should clean the the resources, release media recorder and camera and all other objects.

2) Error in Writing to Output file

We have to set output file to MediaRecorder when we start recording. When you stop all the recorded data will be appended in output file specified by you on start. This could failed due to reasons like insufficient space to write file or you don't have permissions to write to external and internal storage due in Android 6.0 on words.

3) MisMatch in preview size and video size set in MediaRecorder

I have mentioned about this in my previous blog. You can read it here.

Android MediaRecorder Start Failed - 19

Hope this helps you and save you time.

Friday, December 30, 2016

Android MediaRecorder Start Failed - 19

If you have ever used Android MediaRecorder to record video or audio in your android application, you must have faced this error. Recently I had nightmare in using MediaRecorder in my android application. I think it's most unstable class in Android SDK and there is a different behavior in each device. In this blog I am going to explain more about this and possible resolution of this.

First lets see wha's the meaning of this error : Android MediaRecorder Start Failed - 19

There is no documentation on Android developer site about this error code but from my experience I found out that it's because of mis match in size of video preview frame and MediaRecorder video size and this error comes randomly in different devices. The best approach is to use CamCorder profile as mentioned on Android developer site.

CamcorderProfile profile =CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setProfile(profile);

When you use this most of the following default values are set in MediaRecorder from the selected profile.


  • OutputFormat
  • AudioEncoder
  • VideoEncoder
  • VideoSize
  • VideoFrameRate

But this does not work on all the devices as some the devices don't accept these default values hence MediaRecorder does not work and specifically due to VideoSize it gives error. So to solve this you should manually set all the values. Following settings works on almost all devices

int width = mCamera.getParameters().getPreviewSize().width;
int height = mCamera.getParameters().getPreviewSize().height;

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setVideoFrameRate(30);

However there are still some problem with it. When I tested in some high resolution devices. I got following width and height from getPreviewSize()

2048
1536

But MediaRecorder failed to start and when I checked error log I got error.

Unsupported Video Dimension 1920 X 1688

That means it changed the dimension by itself and ignored the width and height I set it in setVideoSize. So that means you can not depend on it. So what's the solution for this. Well the solution is find the optimal video size based on your preferred frame dimensions and available supported size in your device. Following is the function I used for it.

private Size getOptimalPreviewSize(List sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null) {}
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}


List sizes = mCamera.getParameters().getSupportedPreviewSizes();
Camera.Size optimal = getOptimalPreviewSize(sizes, 640, 480);

if(optimal != null){
           
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mMediaRecorder.setVideoSize(optimal.width, optimal.height);
mMediaRecorder.setVideoFrameRate(30);      

}else{

int width = mCamera.getParameters().getPreviewSize().width;
int height = mCamera.getParameters().getPreviewSize().height;

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setVideoFrameRate(30);      

}    

For me above solution worked on almost all devices, first we get optimal size from available supported sizes and if there is no supported sizes then we use default preview size.

Tuesday, December 6, 2016

Android SDK Missing Google Play Services In Revision 30

Recently I faced an issue with my Android SDK, I updated Google Play Services to Revision 30 using SDK manager from eclipse and after update sdk/extras/google/google_play_services/libproject folder was missing and all of my projects gave me error where I have added google play services lib was added.

Now this is bit strange. I checked my SDK folder and find out that instead of 

sdk/extras/google/google_play_services/libproject 

We now have

/sdk/extras/google/m2repository/com/google/android/gms

Where we have play services folder, inside which there are various version folders and it has .aar and .pom file. 

I was not sure how to use that with Eclipse. One option was to create jar file form aar file and add it external lib. But somehow it did not worked. 

I am not sure why Google has break down Google Play Services like this. It may be because Android Studio is now official IDE for android development and eclipse is no longer supported. However my problem was still not resolved as I have native eclipse project and in migrating it to Android Studio, I was facing lots of issues. 

Finally I found out solution and that is to switch to revision 28. You can download Google Play Services Revision 28 from Google Repository. Here is the link.

https://dl-ssl.google.com/android/repository/google_play_services_8298000_r28.zip


Download and extract it to separate folder and import it to your eclipse project. That shall solve your issue. 

Hope this post helps you.