在本文中,您将会了解到关于如何从AndroidJava中的网页获取信息的新资讯,同时我们还将为您解释android获取网页内容的相关在本文中,我们将带你探索如何从AndroidJava中的网页获取信息
在本文中,您将会了解到关于如何从Android Java中的网页获取信息的新资讯,同时我们还将为您解释android获取网页内容的相关在本文中,我们将带你探索如何从Android Java中的网页获取信息的奥秘,分析android获取网页内容的特点,并给出一些关于android – 从网页获取文本到字符串、android – 如何从图像中获取信息?、android – 如何通过条形码获取信息?、android中从java中获取图像时android中的java.lang.OutOfMemoryError的实用技巧。
本文目录一览:- 如何从Android Java中的网页获取信息(android获取网页内容)
- android – 从网页获取文本到字符串
- android – 如何从图像中获取信息?
- android – 如何通过条形码获取信息?
- android中从java中获取图像时android中的java.lang.OutOfMemoryError
如何从Android Java中的网页获取信息(android获取网页内容)
香港专业教育学院一直试图从网页上获取信息成字符串到我的Android应用程序.我一直在使用这种方法.
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class DownloadPage {
private static int arraySize;
public static int getArraySize() throws IOException {
URL url = new URL("http://woah.x10host.com/randomfact2.PHP");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String size = br.readLine();
arraySize = Integer.parseInt(size);
return arraySize;
}
}
我什至在我的AndroidManifest.xml文件中包含了权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
但是,我一直收到错误,我的应用程序无法启动.每当我调用方法或类时,它就会崩溃.
解决方法:
您似乎正在获取android.os.networkonmainthreadException.
请尝试使用AsyncTask来获取该整数.
public class DownloadPage {
private static int arraySize;
public void getArraySize() throws IOException {
new RetrieveInt().execute();
}
private class RetrieveInt extends AsyncTask<String, Void, Integer> {
@Override
protected Integer doInBackground(String ... params) {
try {
URL url = new URL("http://woah.x10host.com/randomfact2.PHP");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String size = br.readLine();
arraySize = Integer.parseInt(size);
} catch (Exception e) {
//do something
}
return arraySize; // gets 18
}
protected void onPostExecute(Integer i) {
// Todo: do something with the number
// You would get value of i == 18 here. This methods gets called after your doInBackground() with output.
System.out.println(i);
}
}
}
android – 从网页获取文本到字符串
我是Android新手,我希望将整个文本从网页变为字符串.我发现了很多像这样的问题,但正如我所说,我是Android新手,我不知道如何在我的应用程序中使用它们.我收到了错误.只有一种方法我设法使它工作,它使用WebView和JavaScript,它很慢,因为地狱.有人可以告诉我一些其他的方法来做到这一点或如何加快WebView,因为我根本不使用它来查看内容.
顺便说一下,我添加了以下代码来加速WebView
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBlockNetworkImage(true);
webView.getSettings().setJavaScriptCanopenWindowsAutomatically(false);
webView.getSettings().setPluginsEnabled(false);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setSavePassword(false);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
如果您知道其他比使用WebView更好更快的解决方案,请告诉我主要活动的完整源代码或解释我应该写的地方,这样我就不会出错.
解决方法:
用这个:
public class ReadWebpageAsyncTask extends Activity {
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.TextView01);
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printstacktrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(Html.fromHtml(result));
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.google.com" });
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button>
<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView>
</LinearLayout>
android – 如何从图像中获取信息?
我尝试将图像细节添加到我的应用程序中,例如拍摄日期,相机型号,宽度,高度,描述….任何人都可以帮助我如何做到这一点.
谢谢.
解决方法:
使用ExifInterface可以获取捕获图像的属性,如果您的相机配置为写入它们.有关详细信息,请参阅link.http://developer.android.com/reference/android/media/ExifInterface.html
android – 如何通过条形码获取信息?
解决方法
以下是他们提供的数据的样本记录:http://www.upcdatabase.com/item/0081697521221
请仔细阅读他们的ToS:http://www.upcdatabase.com/docs/terms.asp
android中从java中获取图像时android中的java.lang.OutOfMemoryError
我正在使用代码从图库中选择一张图片
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.movetoFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
Log.v("picturePath", "picturePath: " + picturePath);
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Intent intentUpload = new Intent(galleryActivity.this, UploadActivity.class);
// intentUpload.putExtra("BitmapImage", bitmap);
MyApplicationGlobal.bitmap = bitmap;
startActivity(intentUpload);
finish();
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.movetoFirst();
return cursor.getString(column_index);
}
但是我收到了错误
FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
at com.markphoto_activities.galleryActivity.onActivityResult(galleryActivity.java:59)
at android.app.Activity.dispatchActivityResult(Activity.java:4541)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2740)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2787)
at android.app.ActivityThread.access$2000(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1032)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4025)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Log-cat是
: E/dalvikvm-heap(2487): Out of memory on a 17915920-byte allocation.
"main" prio=5 tid=1 RUNNABLE
| group="main" sCount=0 dsCount=0 obj=0x400d5638 self=0x126c8
| sysTid=2487 nice=0 sched=0/0 cgrp=default handle=-1342909336
| schedstat=( 2650512000 283442000 1204 ) utm=226 stm=39 core=1
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
at com.markphoto_activities.galleryActivity.onActivityResult(galleryActivity.java:59)
at android.app.Activity.dispatchActivityResult(Activity.java:4541)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2740)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2787)
at android.app.ActivityThread.access$2000(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1032)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4025)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
: D/skia(2487): libjpeg error 105 < Ss=%d, Se=%d, Ah=%d, Al=%d> from allocPixelRef [3456 2592]
: D/skia(2487): --- decoder->decode returned false
: D/AndroidRuntime(2487): Shutting down VM
解决方法:
尝试这个 :
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
try {
picImageView.setimageBitmap(decodeUri(selectedImage));
} catch (FileNotFoundException e) {
e.printstacktrace();
}
}
}
}
然后声明以下函数:
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
final int required_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < required_SIZE || height_tmp / 2 < required_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}
谢谢.
今天关于如何从Android Java中的网页获取信息和android获取网页内容的分享就到这里,希望大家有所收获,若想了解更多关于android – 从网页获取文本到字符串、android – 如何从图像中获取信息?、android – 如何通过条形码获取信息?、android中从java中获取图像时android中的java.lang.OutOfMemoryError等相关知识,可以在本站进行查询。
本文标签: