在模拟器上执行时出现此异常。
我认为这与一些内存问题有关。因为当我在我的手机上尝试它时它工作正常。
有人帮我找到这个问题的真正原因吗?
Galleryview.java
package example.hitwallhd;
import example.hitwallhd.ImageDownloader;
import example.hitwallhd.ImageDownloader.ImageLoaderListener;
import example.hitwallhd.R;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
public class Galleryview extends Activity {
private ProgressBar pb;
private Button save,bnxt,bprv;
private ImageView img;
private static Bitmap bmp;
private TextView percent;
private FileOutputStream fos;
private ImageDownloader mDownloader;
TextView t1,t2;
String num,cate,addrs,urladdrs,pviews,pdown,sele;
int savecount=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galleryview);
save.setEnabled(false);
initViews();
Bundle extras = getIntent().getExtras();
num = extras.getString("num");
cate = extras.getString("category");
sele = extras.getString("sele");
addrs= extras.getString("picno");
t1 =(TextView) findViewById(R.id.textView2);
t2 =(TextView) findViewById(R.id.textView3);
//t1.setText(pviews);
//t2.setText(pdown);
urladdrs="http://someaddrs.com/wallpapers/"+cate+"/"+addrs;
/*--- instantiate our downloader passing it required components ---*/
mDownloader = new ImageDownloader(urladdrs, pb, save, img, percent, Galleryview.this, bmp, new ImageLoaderListener() {
@Override
public void onImageDownloaded(Bitmap bmp) {
Galleryview.bmp = bmp;
/*--- here we assign the value of bmp field in our Loader class
* to the bmp field of the current class ---*/
}
});
/*--- we need to call execute() since nothing will happen otherwise ---*/
mDownloader.execute();
save.setEnabled(true);
}
private void initViews() {
save = (Button) findViewById(R.id.save);
//bnxt=(Button) findViewById(R.id.bnext);
//bprv=(Button) findViewById(R.id.bprev);
/*--- we are using 'this' because our class implements the OnClickListener ---*/
img = (ImageView) findViewById(R.id.imageView1);
img.setScaleType(ScaleType.CENTER_CROP);
pb = (ProgressBar) findViewById(R.id.pbDownload);
pb.setVisibility(View.INVISIBLE);
percent = (TextView) findViewById(R.id.textView1);
percent.setVisibility(View.INVISIBLE);
}
public void onclick_next(View v)
{
bprv.setEnabled(true);
GetData obj = new GetData();
int nxt=Integer.parseInt(num)+1;
num=String.valueOf(nxt);
String urls="http://someaddrs.com/wallpapers/newlist.php?cate="+cate+"&no="+num+"&prev=0";
obj.execute(urls);
}
public void onclick_prev(View v)
{
bnxt.setEnabled(true);
GetData obj = new GetData();
int nxt=Integer.parseInt(num)-1;
num=String.valueOf(nxt);
String urls="http://someaddrs.com/wallpapers/newlist.php?cate="+cate+"&no="+num+"&prev=1";
obj.execute(urls);
}
public void onclick_save(View v)
{
saveImageToSD();
String cnum;
if(urladdrs.equals("nexterror"))
{
int nxt=Integer.parseInt(num)-1;
cnum=String.valueOf(nxt);
}
else if(urladdrs.equals("preverror"))
{
int nxt=Integer.parseInt(num)+1;
cnum=String.valueOf(nxt);
}
else
cnum=num;
savecount=1;
GetData obj = new GetData();
String urls="http://someaddrs.com/wallpapers/downloadcount.php?no="+cnum;
obj.execute(urls);
}
private void saveImageToSD() {
/*--- this method will save your downloaded image to SD card ---*/
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
/*--- you can select your preferred CompressFormat and quality.
* I'm going to use JPEG and 100% quality ---*/
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
/*--- create a new file on SD card ---*/
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + num+"Image.jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/*--- create a new FileOutputStream and write bytes to file ---*/
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bytes.toByteArray());
fos.close();
Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Intent pview = new Intent(Galleryview.this,HitWall.class);
startActivity(pview);
}
public void looperfn()
{
t1 =(TextView) findViewById(R.id.textView2);
t2 =(TextView) findViewById(R.id.textView3);
t1.setText(pviews);
t2.setText(pdown);
if(urladdrs.equals("nexterror"))
{
int nxt=Integer.parseInt(num)-1;
num=String.valueOf(nxt);
bnxt.setEnabled(false);
}
else if(urladdrs.equals("preverror"))
{
int nxt=Integer.parseInt(num)+1;
num=String.valueOf(nxt);
bprv.setEnabled(false);
}
else
{
//t1.setText(urladdrs);
urladdrs="http://someaddrs.com/wallpapers/"+cate+"/"+urladdrs;
mDownloader = new ImageDownloader(urladdrs, pb, save, img, percent, Galleryview.this, bmp, new ImageLoaderListener() {
@Override
public void onImageDownloaded(Bitmap bmp) {
Galleryview.bmp = bmp;
/*--- here we assign the value of bmp field in our Loader class
* to the bmp field of the current class ---*/
}
});
/*--- we need to call execute() since nothing will happen otherwise ---*/
mDownloader.execute();
}
}
public class GetData extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
BufferedReader reader =null;
String data =null;
try{
HttpClient client = new DefaultHttpClient();
URI uri=new URI(params[0]);
HttpGet get =new HttpGet(uri);
HttpResponse response= client.execute(get);
InputStream stream=response.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer =new StringBuffer("");
String line="";
while((line=reader.readLine())!=null){
buffer.append(line);
}
reader.close();
data = buffer.toString();
if(data.equals("preverror")||data.equals("nexterror"))
{
return data;
}
else
{
pviews=data.substring(data.indexOf("|")+1,data.indexOf(":"));
pviews=" Views : "+pviews;
pdown=data.substring(data.indexOf(":")+1, data.length());
pdown=" Downloads : "+pdown;
data=data.substring(0, data.indexOf("|"));
return data;
}
//data=data.substring(0, data.indexOf("|"));
//t1.setText(data);
}
catch(URISyntaxException e){
e.printStackTrace();
}
catch(ClientProtocolException f){
f.printStackTrace();
}
catch(IOException g){
g.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e){
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
urladdrs=result;
if(savecount==0)
{
looperfn();
}
else
savecount=0;
}
}
}