Korrekter Zugriff auf Bilder

  • 4 Antworten
  • Letztes Antwortdatum
S

soundofakira

Neues Mitglied
0
Hi zusammen
Ich versuche eine kleine App zu schreiben, die (momentan noch), ein ganz bestimmtes Bild von meinem Handy zu einem Server sendet.

Alles läuft Fehlerfrei durch, allerdings ohne Ergebnis.
So richtig klar komme ich noch nicht mit Eclipse, dass ich genau sehe wo das Problem ist..

Zunächst.
Ist der Pfad auf das Bild überhaupt richtig?

Code:
private final String fileOutput = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Send.jpg";

Ansonsten hier einmal der komplette Client.

Code:
package com.example.sendmypic;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import android.os.AsyncTask;
import android.os.Environment;

public class Async extends AsyncTask<String, Void, Void>
{
	public Socket socket;
	public PrintWriter printWriter;
	int port = 8888;
	SocketAddress a;
	FileOutputStream fos;
	byte[] aByte = new byte[1];
    int bytesRead;
    InputStream is = null;
    private final String fileOutput = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Send.jpg";
	
	protected Void doInBackground(String... params) {
		
		System.out.println("Im Background");
		a = new InetSocketAddress("192.168.0.101",8888);
		
		socket = new Socket();
		
		try {
			socket.connect(a);
		} 
		catch (IOException e) {
			System.out.println("No Connection");
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

        if (is != null) {

            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            try {
                fos = new FileOutputStream( fileOutput );
                bos = new BufferedOutputStream(fos);
                bytesRead = is.read(aByte, 0, aByte.length);

                do {
                        baos.write(aByte);
                        bytesRead = is.read(aByte);
                } while (bytesRead != -1);

                bos.write(baos.toByteArray());
                bos.flush();
                bos.close();
                socket.close();
            } catch (IOException ex) {
               System.out.println("Bullshit");
            }
        }
return null;
	}	
}

Und der passende Server dazu:
Code:
import java.io.*;
import java.net.*;



 public class Server 
 {
	private final static String fileToSend = "C:\\Send.jpg";

    public static void main(String args[]) {

        while (true) {
            ServerSocket welcomeSocket = null;
            Socket connectionSocket = null;
            BufferedOutputStream outToClient = null;

            try {
                welcomeSocket = new ServerSocket(8888);
                connectionSocket = welcomeSocket.accept();
                System.out.println("Client verbunden");
                outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
            } catch (IOException ex) {
            	System.out.println("Client Probleme");
            }

            if (outToClient != null) {
            	System.out.println("nicht null");
                File myFile = new File( fileToSend );
                byte[] mybytearray = new byte[(int) myFile.length()];

                FileInputStream fis = null;

                try {
                    fis = new FileInputStream(myFile);
                } catch (FileNotFoundException ex) {
                    // Do exception handling
                }
                BufferedInputStream bis = new BufferedInputStream(fis);

                try {
                	System.out.println("Reading");
                    bis.read(mybytearray, 0, mybytearray.length);
                    outToClient.write(mybytearray, 0, mybytearray.length);
                    outToClient.flush();
                    outToClient.close();
                    connectionSocket.close();

                    // File sent, exit the main method
                    return;
                } catch (IOException ex) {
                	System.out.println("Kein InPut");
                }
            }
        }
    }
 }

Wie erwähnt.. es läuft alles durch, aber es wird kein Bild auf meiner Platte geschrieben :(
 
Hast du im Manifest auch die richtigen Permissions angefordert?
 
Muss sonst gäbe es ja exceptions.

Gesendet von meinem GT-S5660 mit der Android-Hilfe.de App
 
bei fehlender WRITE_EXTERNAL_STORAGE permission kommt keine exception
 
  • Danke
Reaktionen: soma-web
Aber bei fehlender Internet permission. Jedenfalls einmal bei mir.

Gesendet von meinem GT-S5660 mit der Android-Hilfe.de App
 

Ähnliche Themen

K
Antworten
0
Aufrufe
327
kgott
K
D
Antworten
8
Aufrufe
751
jogimuc
J
Zurück
Oben Unten