Reputation: 4043
I've developed an Android-application, which parses a web-page and as a result return pictures and texts from it. But when I show my application code to my customer, he said, he want me to write my application using more easy algorithm: application must find picture-urls and text-urls, and then show a content using founded urls. How to do it?
This a parsing class from my application:
public class NetworkConnect extends AsyncTask<String, Void, Void>
{
//Background processing
ProgressDialog parsingBar = ProgressDialog.show(StackParser.this, "Working...", "requesting to URL and parsing content", true, false);
protected Void doInBackground(String... arg)
{
try
{
Log.d(LOG_TAG, arg[0]);
Document doc;
Elements urls;
Elements data;
Intent intent = new Intent(StackParser.this, AvailableContent.class);
doc = Jsoup.connect(arg[0]).timeout(10000).get();
data = doc.getAllElements();
Log.d(LOG_TAG, "Data: " + data.toString());
stringData = doc.text();
Log.d(LOG_TAG, "String data: " + stringData.toString());
urls = doc.select("[href$=.png]");
imageURL = urls.get(0).attr("href");
Log.d(LOG_TAG, "imageURL = " + imageURL);
if(!stringData.equals(""))
{
intent.putExtra("Send to the content-activity", stringData);
intent.putExtra("Send imagesURLs to the content-activity", imageURL);
startActivity(intent);
finish();
}
else
{
intent.putExtra("Send to the content-activity", "empty");
startActivity(intent);
finish();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} //catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//}
return null;
}
protected void onPostExecute(Void result)
{
//Убираем диалог загрузки
parsingBar.dismiss();
}
}
}
Upvotes: 1
Views: 106
Reputation: 580
Use this code:
image.setImageBitmap(decodeSampledBitmapFromUri(url, 200, 200));
public static Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
@Override
public boolean onNavigationItemSelected(int arg0, long arg1) {
// TODO Auto-generated method stub
return false;
}
Upvotes: 1
Reputation: 13
Have you tried with
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
How to display image from URL on Android
Upvotes: 1