2017-05-23 13 views
0

私はsocket.ioの初心者です。私は、我々が正常にブラウザを使用して画像をアップロードした画像を送信ソケットioとアンドロイド

https://www.npmjs.com/package/socket.io-stream

ライブラリを使用されています。しかし、今、私はアンドロイドアプリケーションから画像をアップロードしたいと思います。誰もがAndroidのコードがある場合は私に与えてください。..

https://github.com/socketio/socket.io-client-java/issues/29

を私はGoogleで検索しますが、適切な解決策を見つけていないされています。あなたはbase64文字列として画像を送信する必要がアンドロイド使ってソケットからのアップロードのイメージのために

答えて

0

後は、あなたが別のPARAMATERSと同じデータを送信するbase64でに変換イメージのための一例です。アンドロイドで

String base64Image = getBase64Data(dirPath + "/" + fileName); 

public String getBase64Data(String filePath) { 
     try { 
      InputStream inputStream = new FileInputStream(filePath);//You can get an inputStream using any IO API 
      byte[] bytes; 
      byte[] buffer = new byte[8192]; 
      int bytesRead; 
      ByteArrayOutputStream output = new ByteArrayOutputStream(); 
      try { 
       while ((bytesRead = inputStream.read(buffer)) != -1) { 
        output.write(buffer, 0, bytesRead); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      bytes = output.toByteArray(); 
      return "data:image/jpeg;base64," + Base64.encodeToString(bytes, Base64.DEFAULT); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return ""; 
    } 
+0

socket-io-streamライブラリをご利用ください。これはnpmパッケージです。あなたのコードはこのライブラリで動作しますか? –

+0

私はコード関連ソケットを一切行っていません。ここで提案しました。文字列で変換するだけでイメージデータを送ることができます。 –

0

は、あなたは、サーバー側ではBase64で

public void sendImage(String path) 
{ 
    JSONObject sendData = new JSONObject(); 
    try{ 
      sendData.put("imageData", encodeImage(path)); 
      socket.emit("image",sendData); 
     }catch(JSONException e){ 

     } 
} 

private String encodeImage(String path) 
{ 
     File imagefile = new File(path); 
     FileInputStream fis = null; 
     try{ 
      fis = new FileInputStream(imagefile); 
     }catch(FileNotFoundException e){ 
      e.printStackTrace(); 
     } 
     Bitmap bm = BitmapFactory.decodeStream(fis); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG,100,baos); 
     byte[] b = baos.toByteArray(); 
     String encImage = Base64.encodeToString(b, Base64.DEFAULT); 
     //Base64.de 
     return encImage; 
} 

を用いて画像を符号化する画像を受信して​​、他のファイルの呼び出しAから、

socket.on("image", function(info) { 
    var img = new Image(); 
    img.src = 'data:image/jpeg;base64,' + info.imageData; 

}); 
3
var imageBuffer = customJs.decodeBase64Image(base64Data); 
    var imageTypeDetected = imageBuffer.type.match(/\/(.*?)$/); 
    var filename = 'profile-' + Date.now() + '.' + imageTypeDetected[1]; 
    // config.uploadImage --- Folder path where you want to save. 
    var uploadedImagePath = config.uploadImage + filename; 
    try { 
     fs.writeFile(uploadedImagePath, imageBuffer.data, function() { 
     dbMongo.updateImage({email: decoded.email, user_id: decoded.userId, 'profile_picture': config.showImagePath + filename}, function (res) { 
     if (res.error) { 
     socket.emit('set_update_image', {'error': 1, 'message': 'Error!' + res.message, 'data': null, 'status': 400}); 
     } else { 
     console.log(res); 
     socket.emit('set_update_image', res); 
     } 
     }); 
    }); 
     } catch (e) { 
      socket.emit('set_update_image', {'error': 1, 'message': 'Internal server error ' + e, 'data': null, 'status': 400}); 
     } 

をデコードする必要があります機能

exports.decodeBase64Image = function decodeBase64Image(dataString) { 
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/); 
var response = {}; 

if (matches.length !== 3) 
{ 
    return new Error('Invalid input string'); 
} 

response.type = matches[1]; 
response.data = new Buffer(matches[2], 'base64'); 

return response; 
} 
+0

ありがとうyogesh。働く良い。しかし、base64は5MB以上のファイルはアップロードされません。何か考えてください。 –

関連する問題