应用程序仅在某些手机上崩溃(连接到互联网时?)

时间:2013-07-30 21:54:44

标签: android http crash

我是唯一一个可以成功将图片从图库上传到应用程序在线的人...对于其他人来说,手机似乎在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){

    }
}


}

2 个答案:

答案 0 :(得分:1)

您正在主UI线程上进行网络调用。

这可能适用于您的手机,因为您有一个旧的Android版本(我猜姜饼)。

从API级别11开始,这将抛出NetworkOnMainThreadException

<强>解决方案: 网络调用只能从后台线程进行。

我建议使用AsyncTaskExample

答案 1 :(得分:1)

有点晚了,我知道。但正在发生的事情是新手机拍摄的图片质量更高,而且在上传过程中,我猜图片的尺寸太大了。所以我不得不事先减少它们。

BitmapFactory.Options opts = new 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);