Java 在Android中获取图片url和文本url
我开发了一个Android应用程序,它解析网页,结果返回图片和文本。但当我向客户展示我的应用程序代码时,他说,他希望我使用更简单的算法编写我的应用程序:应用程序必须找到图片URL和文本URL,然后使用创建的URL显示内容。怎么做 这是我的应用程序中的分析类:Java 在Android中获取图片url和文本url,java,android,Java,Android,我开发了一个Android应用程序,它解析网页,结果返回图片和文本。但当我向客户展示我的应用程序代码时,他说,他希望我使用更简单的算法编写我的应用程序:应用程序必须找到图片URL和文本URL,然后使用创建的URL显示内容。怎么做 这是我的应用程序中的分析类: public class NetworkConnect extends AsyncTask<String, Void, Void> { //Background processing
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();
}
}
}
公共类NetworkConnect扩展异步任务
{
//背景处理
ProgressDialog parsingBar=ProgressDialog.show(StackParser.this,“工作…”,“请求URL并解析内容”,true,false);
受保护的Void doInBackground(字符串…arg)
{
尝试
{
Log.d(Log_标记,arg[0]);
文件文件;
元素URL;
元素数据;
Intent Intent=新的Intent(StackParser.this,AvailableContent.class);
doc=Jsoup.connect(arg[0]).timeout(10000.get();
数据=doc.getAllegements();
Log.d(Log_标记,“Data:+Data.toString());
stringData=doc.text();
d(Log_标记,“字符串数据:+stringData.toString());
URL=doc.select(“[href$=.png]”);
imageURL=URL.get(0.attr(“href”);
Log.d(Log_标签,“imageURL=“+imageURL”);
如果(!stringData.equals(“”)
{
intent.putExtra(“发送到内容活动”,stringData);
putExtra(“将imagesURLs发送到内容活动”,imageURL);
星触觉(意向);
完成();
}
其他的
{
intent.putExtra(“发送到内容活动”,“空”);
星触觉(意向);
完成();
}
}
捕获(IOE异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}//捕获(中断异常e){
//TODO自动生成的捕捉块
//e、 printStackTrace();
//}
返回null;
}
受保护的void onPostExecute(void结果)
{
//Убираем диалог загрузки
parsingBar.disclose();
}
}
}
你试过了吗
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;
}
使用以下代码:
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;
}
您是否从该异步任务获取URL???@BalvinderSingh,否,该异步任务解析web page.Log.d(Log_标记,“imageURL=“+imageURL”);这行打印的是什么???@BalvinderSingh,它打印的是url,它与网页上的图片相连。如果您想从url获取位图,请使用我的答案