bugfix> android > 投稿

sslソケットとバッファーを使用してbyte []をサーバーに送信しようとしましたが、バッファーに成功しません。 私はよく検索し、いくつかのトピックが私のもののように見えることを確認しますが、それらのコードを試します(私の場合に合わせて)が、成功しません。ご協力ありがとうございます。

クライアント側の編集(Android):

byte[] bytes = null;
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    bytes = stream.toByteArray();
    File file = new File(String.valueOf(bytes));
    ImageView mImageView = findViewById(R.id.imageView2);
    Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),
            R.drawable.test);
    mImageView.setImageBitmap(bitmap);
    if (socketConnect()) {
        try {
            Log.d("MainActivity", "Before OutputStream");
            OutputStream os = sock.getOutputStream();
            os.write(bytes);
            os.flush();
            os.close();
            //in.close();
            sock.close();
            Log.d("MainActivity", "End");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else {
        Log.e("MainActivity", "Erreur");
    }

サーバー側(python):

import socket
import sys
import ssl

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("",9999))
sock.listen(10)
i=1
while True:
    (clientsocket, (ip, port)) = sock.accept()
    connstream = ssl.wrap_socket(clientsocket,
                                 server_side = True,
                                 certfile = "server.crt",
                                 keyfile = "server.key")
    f = open('file_'+str(i)+".png",'wb') #open in binary
    i=i+1
    print(i)
    l = 1
    l = connstream.read(1024)
    while (l):
        print("toto")
        f.write(l)
        l = connstream.read(1024)
    f.close()
    connstream.close()
sock.close()

コードを実行すると、FileInputStreamが機能しないようです

助けてくれてありがとう

編集:

Logcat:

05-25 15:41:45.993 16257-16257/? I/zygote: Not late-enabling -Xcheck:jni (already on)
05-25 15:41:46.020 16257-16257/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
05-25 15:41:46.477 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
I/InstantRun: starting instant run server: is main process
05-25 15:41:46.952 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
E/MainActivity: debut connexion socket
Choix bks version
05-25 15:41:46.953 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
E/MainActivity: Bks recup
05-25 15:41:46.978 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
E/MainActivity: Connexion socket
05-25 15:41:46.978 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
D/MainActivity: Before OutputStream
05-25 15:41:47.002 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
D/MainActivity: Before FileInputStream
05-25 15:41:47.002 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
W/System.err: java.io.FileNotFoundException: [B@838f46d (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:200)
    at java.io.FileInputStream.<init>(FileInputStream.java:150)
    at com.youstiti.simon.testenvoiwithbuffer.MainActivity.onCreate(MainActivity.java:115)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
05-25 15:41:47.003 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
05-25 15:41:47.021 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/OpenGLRenderer: HWUI GL Pipeline
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
I/OpenGLRenderer: Initialized EGL, version 1.4
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/OpenGLRenderer: Swap behavior 1
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
W/OpenGLRenderer: Failed to choose config with 
EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/OpenGLRenderer: Swap behavior 0
05-25 15:41:47.052 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/EGL_emulation: eglCreateContext: 0xa5a31e60: maj 2 min 0 rcv 2
05-25 15:41:47.061 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/EGL_emulation: eglMakeCurrent: 0xa5a31e60: ver 2 0 (tinfo 0x99aa9d20)
05-25 15:41:47.101 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/EGL_emulation: eglMakeCurrent: 0xa5a31e60: ver 2 0 (tinfo 0x99aa9d20)

LOGCAT OUTOFMEMORYの編集:

--------- beginning of crash
05-25 16:24:02.845 14571-14571/com.youstiti.simon.youstititest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.youstiti.simon.youstititest, PID: 14571
java.lang.OutOfMemoryError: Failed to allocate a 509239740 byte allocation with 33554432 free bytes and 445MB until OOM
    at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:83)
    at java.lang.StringBuilder.<init>(StringBuilder.java:67)
    at java.util.Arrays.toString(Arrays.java:2062)
    at com.youstiti.simon.youstititest.activity.GalleryActivity$Upload.doInBackground(GalleryActivity.java:145)
    at com.youstiti.simon.youstititest.activity.GalleryActivity$Upload.<init>(GalleryActivity.java:94)
    at com.youstiti.simon.youstititest.activity.GalleryActivity.onActivityResult(GalleryActivity.java:84)
    at android.app.Activity.dispatchActivityResult(Activity.java:6463)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3738)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3785)
    at android.app.ActivityThread.access$1400(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5525)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

回答 1 件
  • File file = new File(String.valueOf(bytes));

    それはナンセンスです。メモリにバイトがあります。 Fileクラスは必要ありません

    FileInputStream in = new FileInputStream(bytes);

    それもナンセンスです。 FileInputStreamは必要ありません。次に、この方法でバイトに使用することはできません。それはコンパイルされますか?本当に?

    あなたはそれをすべてやめることができます。必要なものは次のとおりです。

    os.write(bytes);
    
    

あなたの答え