我是唯一一个可以成功地将图片从画廊上传到在线应用程序的人......对于其他人来说,手机似乎在 http 请求时崩溃了,它似乎。这是我的结局还是他们的结局?谢谢..
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();
}
}
}
这是崩溃时的示例。但是,它也会在另一个实例中崩溃。我相信这是当他们使用连接互联网的应用程序的一部分时。
它适用于我的手机,但对其他人无效(我有 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){
}
}
}