第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在Android中上傳大文件而不會(huì)出現(xiàn)內(nèi)存錯(cuò)誤

在Android中上傳大文件而不會(huì)出現(xiàn)內(nèi)存錯(cuò)誤

三國(guó)紛爭(zhēng) 2019-09-02 08:34:04
我的上傳代碼如下:String end = "\r\n";String twoHyphens = "--";String boundary = "*****";try {    URL url = new URL(ActionUrl);    HttpURLConnection con = (HttpURLConnection) url.openConnection();    con.setDoInput(true);    con.setDoOutput(true);    con.setUseCaches(false);    con.setRequestMethod("POST");    con.setRequestProperty("Connection", "Keep-Alive");    con.setRequestProperty("Accept", "text/*");    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);    DataOutputStream ds = new DataOutputStream(con.getOutputStream());    ds.writeBytes(twoHyphens + boundary + end);    ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end);    ds.write(SavePath.getBytes("UTF-8"));    ds.writeBytes(end);    ds.writeBytes(twoHyphens + boundary + end);    ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\"");    ds.write(FileName.getBytes("UTF-8"));    ds.writeBytes("\"" + end);    ds.writeBytes(end);    FileInputStream fStream = new FileInputStream(uploadFilepath+""+FileName);    int bufferSize = 1024;    byte[] buffer = new byte[bufferSize];    int length = -1;    int pro = 0;    while((length = fStream.read(buffer)) != -1) {        ds.write(buffer, 0, length);    }           ds.writeBytes(end);    ds.writeBytes(twoHyphens + boundary + twoHyphens + end);    fStream.close();    ds.flush();    InputStream is = con.getInputStream();    int ch;    StringBuffer b = new StringBuffer();    while((ch = is.read()) != -1) {        b.append((char)ch);    }    ds.close();}catch(Exception e) {    e.printStackTrace();}雖然小于16 MB,但它上傳成功。但是當(dāng)它超過16 mb時(shí),“OutOfMemory”錯(cuò)誤顯示出來。如何避免outofmemory錯(cuò)誤?
查看完整描述

3 回答

?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

如果服務(wù)器接受分塊模式,則可以使用


((HttpURLConnection) con).setChunkedStreamingMode(chunkLength)

否則你可以使用


((HttpURLConnection) con).setChunkedStreamingMode(0);

要么


/* compute first your request content length

   contentLength = formBodyLength + yourFileSize

*/

con.setRequestProperty("Content-Length", String.valueOf(contentLength));

((HttpURLConnection) con).setFixedLengthStreamingMode(contentLength);

終于......發(fā)送你想要的東西


查看完整回答
反對(duì) 回復(fù) 2019-09-02
?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

bitmap=Bitmap.createScaledBitmap(bitmap, 100, 100, true);


ByteArrayOutputStream stream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to format you want.

byte [] byte_arr = stream.toByteArray();  

String image_str = Base64.encodeBytes(byte_arr);

最好的方式我試過它對(duì)我來說是成功的!


查看完整回答
反對(duì) 回復(fù) 2019-09-02
  • 3 回答
  • 0 關(guān)注
  • 603 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)