我一直在努力解决这个问题,我试着打电话给以下人员:
new DownloadandSaveAsync(Activity_picture.this).execute(image_url);
单击ImageView时会调用此方法。
但是一旦我的AsyncTask开始加载它就会关闭,没有崩溃报告,只需以下内容:
Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 6744 (AsyncTask #4)
这是我的活动,我将其称为:
public class Activity_picture extends ActionBarActivity {
public static final int ALBUM_RESULT = 300;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
final String image_url = getIntent().getStringExtra("image_url");
String image_title = getIntent().getStringExtra("image_title");
ImageView picture = (ImageView) findViewById(R.id.picture);
TextView pictureTitle = (TextView) findViewById(R.id.pictureTitle);
Picasso.with(getApplicationContext()).load(image_url).into(picture);
pictureTitle.setText(image_title);
picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadandSaveAsync(Activity_picture.this)
.execute(image_url);
finish();
}
});
}
}
然后这是我的AsyncTask代码:
public class DownloadandSaveAsync extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
Bitmap profPictBitmap;
Context context;
URL image_value;
public DownloadandSaveAsync(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
image_value = new URL(params[0]);
System.out.println(image_value);
profPictBitmap = BitmapFactory.decodeStream(image_value
.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
File f = new File(context.getCacheDir(), "profileImage");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Convert bitmap to byte array
Bitmap bitmap = profPictBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
// write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.write(bitmapdata);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent newIntent = new Intent(context, FeatherActivity.class);
newIntent.setData(Uri.parse(f.toString()));
newIntent.putExtra(Constants.EXTRA_IN_API_KEY_SECRET,
"SECRET KEY");
((Activity) context).startActivityForResult(newIntent, 1);
if (pDialog != null) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
}
我100%确定正在执行的url是不错的,因为我已经完成了System.out.println(image_value);
并且它会像在AsyncTask类中一样返回正确的URL。
我在我的应用程序的其他地方调用了相同的AsyncTask代码来下载图像,并且在传递其他URL时它的工作效率100%,但由于某些奇怪的原因我无法理解,它强制关闭该错误。
如果有人能够在这里帮助我,那将非常感激!提前致谢。
答案 0 :(得分:0)
在我的情况下,我使用 Asynctask 我遇到了同样的错误
<uses-permission android:name="android.permission.INTERNET" />
<块引用>
我只是在清单文件中添加了互联网权限。之后它对我有用