Android 仅在某些手机上应用程序崩溃(连接到互联网时?)
我是唯一一个谁可以成功地将图片从画廊上传到应用程序在线。。。对于其他人来说,手机似乎在http请求时崩溃。这是我这边还是他们那边?谢谢Android 仅在某些手机上应用程序崩溃(连接到互联网时?),android,http,crash,Android,Http,Crash,我是唯一一个谁可以成功地将图片从画廊上传到应用程序在线。。。对于其他人来说,手机似乎在http请求时崩溃。这是我这边还是他们那边?谢谢 public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.activity_main); Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.icon2);
Bitmap bitmapOrgg = BitmapFactory.decodeFile(BrowsePicture.selectedImagePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
bitmapOrgg.compress(Bitmap.CompressFormat.JPEG, 30, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://asdfasdf.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Log.d("1","1");
}
try{
Class ourClass = Class.forName("www.xxx.com");
Intent ourIntent = new Intent(upload.this, ourClass);
startActivity(ourIntent);
Log.d("2","2");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
public void onCreate(捆绑冰柱){
超级冰柱;
setContentView(R.layout.activity_main);
位图bitmapOrg=BitmapFactory.decodeResource(getResources(),
R.可拉拔的icon2);
位图bitmapOrgg=BitmapFactory.decodeFile(BrowsePicture.selectedImagePath);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
//压缩(Bitmap.CompressFormat.JPEG,90,bao);
Bitmaporg.compress(Bitmap.CompressFormat.JPEG,30,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs=新建
ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新
HttpPost(“http://asdfasdf.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
日志d(“1”、“1”);
}
试一试{
Class ourClass=Class.forName(“www.xxx.com”);
Intent-ourIntent=新的Intent(upload.this,ourClass);
星触觉(ourIntent);
日志d(“2”、“2”);
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
}
这是它崩溃的一个例子。但是,它在另一个实例中也会崩溃。我相信这是当他们使用应用程序中连接互联网的部分时
它可以在我的手机上使用,但对其他任何人都不起作用(我有一个S1 Blaze,他们有Nexus和Galaxy S4)
更新:
try{
Class ourClass = Class.forName("com.x.x.asdf.");
Intent ourIntent = new Intent(upload.this, ourClass);
startActivity(ourIntent);
Log.d("2","2");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
loadSomeStuff uploader= new loadSomeStuff();
// since the first param in <Void,Void,Void> you do not send in anything in execute.
uploader.execute("test");
Log.e("LOG", BrowsePicture.selectedImagePath);
}
public class loadSomeStuff extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.icon2);
Bitmap bitmapOrgg = BitmapFactory.decodeFile(BrowsePicture.selectedImagePath);
Log.e(BrowsePicture.selectedImagePath, "yo");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
bitmapOrgg.compress(Bitmap.CompressFormat.JPEG, 30, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://website.com/php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Log.d("1","1");
}
return "test";
}
@Override
protected void onPostExecute(String pinny){
}
}
}
试试看{
Class-ourClass=Class.forName(“com.x.x.asdf”);
Intent-ourIntent=新的Intent(upload.this,ourClass);
星触觉(ourIntent);
日志d(“2”、“2”);
}catch(classnotfounde异常){
e、 printStackTrace();
}
loadSomeStuff uploader=新的loadSomeStuff();
//由于中的第一个参数,所以在execute中不发送任何内容。
执行(“测试”);
Log.e(“Log”,浏览图片。选择图像路径);
}
公共类loadSomeStuff扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
位图bitmapOrg=BitmapFactory.decodeResource(getResources(),
R.可拉拔的icon2);
位图bitmapOrgg=BitmapFactory.decodeFile(BrowsePicture.selectedImagePath);
Log.e(BrowsePicture.selectedImagePath,“yo”);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
//压缩(Bitmap.CompressFormat.JPEG,90,bao);
Bitmaporg.compress(Bitmap.CompressFormat.JPEG,30,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs=新建
ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新
HttpPost(“http://website.com/php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
日志d(“1”、“1”);
}
返回“测试”;
}
@凌驾
受保护的void onPostExecute(字符串pinny){
}
}
}
您正在主UI线程上进行网络调用
这可能适用于你的手机,因为你有一个旧的android版本(我猜是姜饼)
从API级别11开始,这将抛出
解决方案:
只能从后台线程进行网络调用
我建议使用一种新的方法 我知道有点晚了。但是现在的情况是,新手机拍摄的照片质量更高,在上传过程中,我猜图像的大小太大了。所以我必须事先减少它们 BitmapFactory.Options opts=新的BitmapFactory.Options(); opts.inJustDecodeBounds=true
BitmapFactory.decodeFile(BrowsePicture.selectedImagePath, opts);
int width = opts.outWidth;
int height = opts.outHeight;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
opts.inDither = true;
opts.inJustDecodeBounds = false;
opts.inSampleSize = (int)Math.pow(2.0,Math.floor(Math.log(2)/Math.log(2)));//for example double scale_factor=(double)width/desired_dimension;
Bitmap bitmapOrgg = BitmapFactory.decodeFile(BrowsePicture.selectedImagePath,opts);
请注意。。发布logcat输出以将超时设置得更高。。谢谢你的帮助。我试过了,它所做的一切似乎都是让他们的手机在稍晚的时候崩溃,或者是在连接失败或其他什么之后崩溃。。。一些更新的代码:如果你仍然有同样的问题,请张贴你的错误日志。您可以在LogCat视图中找到它。