我制作了一款小型Android应用,可以使用我的应用在Google图片上搜索图片。
最初我正在测试在src
中打印图像TextView
。
但该应用程序会因提升SecruityException
而停止。
package com.example.googleimages;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
ProgressBar progress;
Button btn;
EditText searchtext;
Document doc;
TextView tv;
String src;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
btn.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
progress=(ProgressBar)findViewById(R.id.progressBar1);
btn=(Button)findViewById(R.id.search);
searchtext=(EditText)findViewById(R.id.searchText);
tv=(TextView)findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
progress.setVisibility(0);
for(int i=0;i<100;i+=20)
progress.setProgress(i);
Thread browser=new Thread(){
public void run(){
try {
doc=Jsoup.connect("http://www.google.com.my/imghp?hl=en&tab=wi").data("q",searchtext.getText().toString())
.data("btnG","Search by image").userAgent("Mozilla").get();
Elements elements=doc.getElementsByTag("img");
for(Element e:elements){
src=e.attr("src");
tv.setText(tv.getText()+src+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
browser.start();
}
}