Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

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. 

Friday, September 18, 2015

Android Download and Save Image to Internal Memory

Hello,

Recently in one of my project. We have a requirement to download image from remote URL and save it to internal memory so every time image won't be downloaded but it will be displayed from internal memory.

First let's create a utility class and add static function to load and save image. Following is the UTIL class I used.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Utils {
    public static void saveFile(Context context, Bitmap b, String picName){ 
    FileOutputStream fos; 
    try
        fos = context.openFileOutput(picName, Context.MODE_PRIVATE); 
        b.compress(Bitmap.CompressFormat.PNG, 100, fos); 
        fos.close(); 
    }  
    catch (FileNotFoundException e) { 
        Log.d("file", "file not found"); 
        e.printStackTrace(); 
    }  
    catch (IOException e) { 
        Log.d("file", "io exception"); 
        e.printStackTrace(); 
    } 

}
    
    public static Bitmap loadBitmap(Context context, String picName){ 
    Bitmap b = null
    FileInputStream fis; 
    try
        fis = context.openFileInput(picName); 
        b = BitmapFactory.decodeStream(fis); 
        fis.close(); 

    }  
    catch (FileNotFoundException e) { 
        Log.d("file", "file not found"); 
        e.printStackTrace(); 
    }  
    catch (IOException e) { 
        Log.d("file", "io exception"); 
        e.printStackTrace(); 
    }  
    return b; 
}

}

As you can see above it has two functions one is to save image and one is to load saved image. Now first we will check if if image is already saved or not.

ImageView imgView = new Image(getActivity());
Bitmap b = Utils.loadBitmap(getActivity(),"myImage.png");
if(b == null){
       new DownloadImageTask(imgView,"myImage.png").execute("http://remoteimagepath");
}else{
      imgView.setImageBitmap(b);
}

Now we have to add DownloadImageTask class as follow.

private class DownloadImageTask extends AsyncTask {
ImageView imageView = null;
        String name = "";
public DownloadImageTask(ImageView img, String name) {
this.imageView = img;
                this.name = name;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
Utils.saveFile(getActivity(),result, this.name);
this.imageView.setImageBitmap(result);
}
}

As you can see above in this class we are passing reference of imageView and when image is downloaded we setup bitmap to imageview and save image to local memory with name which we passed in parameter. 

Hope this helps you.