-1

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

    }
}


}
4

2 回答 2

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);
于 2013-12-30T20:01:13.543 回答
1

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

这可能适用于您的手机,因为您使用的是旧的 android 版本(我猜是 Gingerbread)。

从 API 级别 11 开始,这将引发NetworkOnMainThreadException

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

我建议使用AsyncTask例子

于 2013-07-30T22:13:48.153 回答