2012-02-22 32 views
1

私はアンドロイドからリモートサーバーに画像をアップロードしようとしています。大きな写真を除いて、すべて動作しています。私はBase64.encodeBytes()のメゾで "例外からメモリを取り除いています"。ここにコードがあります。Base64 encodeBytes - メモリ不足例外

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 

この問題の解決にご協力いただけますか?私は以前にビットマップをサンプリングしてこの問題を解決しようとしましたが、それは動作しますが、ビットマップを縮小して、いつもオリジナルのサイズで写真をアップロードしたいと思います。

ありがとうございました。私は前に同じ問題を抱えていた大規模なイメージのために...私はただのBase64に変換...あなたは、次のようにそれを使用する前に、ビットマップを拡大縮小している

+0

PNGは、ロスレス形式です。代わりにJPEGを使用する必要があります – Hrafn

答えて

0

は、私は、サーバーへの画像の正確な元のサイズをアップロードに関する解決策を見つけた:それは品質設定を使用しないように

public static void upload(String pathToOurFile, String serverUrl){ 
    HttpURLConnection connection = null; 
    DataOutputStream outputStream = null; 
    DataInputStream inputStream = null; 

    String urlServer = serverUrl; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 

    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 

    try 
    { 
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 

    URL url = new URL(urlServer); 
    connection = (HttpURLConnection) url.openConnection(); 

    // Allow Inputs & Outputs 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 

    // Enable POST method 
    connection.setRequestMethod("POST"); 

    connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

    outputStream = new DataOutputStream(connection.getOutputStream()); 
    outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
    outputStream.writeBytes(lineEnd); 

    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    buffer = new byte[bufferSize]; 

    // Read file 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

    while (bytesRead > 0) 
    { 
    outputStream.write(buffer, 0, bufferSize); 
    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    } 

    outputStream.writeBytes(lineEnd); 
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

    // Responses from the server (code and message) 
    int serverResponseCode = connection.getResponseCode(); 
    String serverResponseMessage = connection.getResponseMessage(); 

    fileInputStream.close(); 
    outputStream.flush(); 
    outputStream.close(); 
    } 
    catch (Exception ex) 
    { 
    //Exception handling 
    } 
} 
+0

この方法はwebview posturlで使用できますか? – Sunny

1

...

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

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want. 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 
+0

しかし、そのスケーリングはbimap儀式??元の画像にアップロードしたいと思います。 –

+0

はいこれは画像の品質を落とす –

0

を変更しようとすることができます私はちょうど10 &に与えているように圧縮の品質パラメータは、それも大規模な画像のために正常に動作します。これらの記述に基づいて

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 10, stream); 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 
+0

私はそれを試してみましょう。応答のためのThnx。 –

+0

申し訳ありませんが、動作しませんでした。例外はまだ投げられています。 –

+0

これはあなたのプロジェクトでインポートされるものですimport android.util.Base64;はいの場合は、次のURLを使用して、そのBase64.javaをプロジェクトにインポートしてから、チェックしてください。 http://androidcodemonkey.blogspot.in/2010/03/how-to-base64-encode-decode-android.html – Sumant

0

:リモートサーバへのアンドロイドから写真をアップロードしようと

1 - Iミリアンペア。

2-スケーリングはbimap riteですか?元画像の にアップロードします

私は解決策があなたがアンドロイドアプリケーションからサーバーにファイルをアップロードするのを助け、元のイメージを保持するのを助ける限り、何でもしますと仮定します。

解決策 - 以下のチュートリアルの圧縮を削除してください。

http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

大きな画像ファイルのBase64.encodeBytesを()は避けてください。