2013-07-30 111 views
-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大火,他們已經有Nexus和銀河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){ 

    } 
} 


} 
+0

你的代碼在哪裏? –

+0

更多詳細信息添加 –

+0

cmon請求。張貼您的logcat輸出 –

回答

1

您正在主UI線程上的網絡調用。

這可能是在你的手機上工作,因爲你有一箇舊的Android版本(我猜薑餅)。

從API級別11開始,這將拋出一個NetworkOnMainThreadException

解決方案: 網絡電話應該只從後臺線程做。

我建議使用AsyncTaskExample

+0

感謝您的幫助。我試了一下,似乎它所做的一切都是讓他們的手機在一段時間後崩潰,或者在連接失敗或其他事情發生之後......一些更新的代碼: –

+0

如果您仍然遇到同樣的問題,請發佈您的錯誤登錄。您可以在LogCat視圖中找到它。 –

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); 
相關問題