package com.ved.musicmapapp.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.ved.musicmapapp.R;
public class DownloadUsersImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
RelativeLayout layout;
ImageView imageView = null;
String artistImage = "";
Context context;
public DownloadUsersImagesTask(Context context) {
super();
this.context = context;
}
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
String imagePath = "";
String artistName = (String) imageView.getTag();
try {
String artist = artistName.replaceAll(" ", "%20");
URL url = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=fdbae8e7fa1d67ea46a8ab7d4dd26fdb&artist=" + artist);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("image")) {
//Log.v("imahe", "...attr..." + xpp.getAttributeName(3));
if (xpp.getAttributeValue(null, "size").equalsIgnoreCase("extralarge")) {
if (imagePath.equalsIgnoreCase("")) {
imagePath = xpp.nextText();
}
}
}
}
eventType = xpp.next(); // move to next element
}
// Log.e("final image", "..." + imagePath);
} catch (Exception e1) {
Log.e("Exception in DownloadUsersImagesTask", "..." + e1.getMessage());
}
if (imagePath.equals("")) {
imagePath = "image not found";
}
String urlPath = imagePath;
Bitmap artistbitmap = download_Image(urlPath);
if(artistbitmap != null){
UtilTask.addBitmapToMemoryCache(imagePath, artistbitmap);
}
// for(int j=0; j<AroundMeActivity.artistArray.size();j++){
// Metadata song = AroundMeActivity.artistArray.get(j);
// if(song.getArtist().equals(artistName)){
// AroundMeActivity.artistArray.get(j).setSongimage(imagePath);
// }
// }
artistImage = imagePath;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Statics.WS_PATH + "Update_Artist.php");
//HttpPost httppost = new HttpPost("http://vedsolution.com/heta/Update_Artist.php");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("song_artist",artistName));
nvps.add(new BasicNameValuePair("song_image",artistImage));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
org.apache.http.HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
Log.v("result update success", "..." + result);
}
}
catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncodingException ", e.getMessage());
}catch (ClientProtocolException e) {
Log.e("ClientProtocolException ", e.getMessage());
} catch (IOException e) {
Log.e("IOException ", e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.e("JSONException ", e.getMessage());
}
return artistbitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if(result == null){
//imageView.setImageResource(R.drawable.images);
android.view.Display display = ((android.view.WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.img_no_image);
int size = (int) (display.getWidth() * 0.95f);
icon = Bitmap.createScaledBitmap(icon, size, size, true);
imageView.setImageBitmap(icon);
}
else{
android.view.Display display = ((android.view.WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int size = (int) (display.getWidth() * 0.95f);
result = Bitmap.createScaledBitmap(result, size, size, true);
imageView.setImageBitmap(result);
}
}
private Bitmap download_Image(String url) {
Bitmap bmp = null;
try {
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
} catch (Exception e) {
}
return bmp;
}
}