0

I'm trying to copy a file to an android device using Wifi. For what i cant tell the creation of the file works, but i can't find it anywhere...

I've try to use the DDMS on Eclipse, but there's nothing on the Data folder... for what i read on other topics, the file should be located on the data folder of the App.. but there's no folder anywhere...

This is the code

package com.ejemplo.cliente;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

 public class ClienteSocket extends Activity {

 Socket socket;
 String IP = "192.168.1.101";
 Integer Puerto = 4444;
 TextView Mensaje;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Mensaje = (TextView)findViewById(R.id.Mensaje);

    try {
         socket = new Socket(IP,Puerto);
         Mensaje.setText("Conectado");
         InputStream input = socket.getInputStream();
         BufferedReader inReader = new BufferedReader(new                  InputStreamReader(socket.getInputStream()));
        BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String filename = inReader.readLine();

      if ( !filename.equals("") ){

          outReader.write("READY\n");
          outReader.flush();
          Mensaje.setText("Request Acepted");

                FileOutputStream wr = openFileOutput(filename,MODE_WORLD_READABLE);
                OutputStreamWriter osw = new OutputStreamWriter(wr); 


          byte[] buffer = new byte[socket.getReceiveBufferSize()];

          int bytesReceived = 0;

          while((bytesReceived = input.read(buffer))>0)
          {

             osw.write(bytesReceived);
             Toast.makeText(this, "Transfering", Toast.LENGTH_SHORT).show();
          }

          osw.flush();
          osw.close();
          Mensaje.setText("File Received");
      }

    } catch (UnknownHostException e) {
      Mensaje.setText("error 1" + e.getMessage());
    } catch (IOException e) {
      Mensaje.setText("error 2" + e.getMessage());
   }

   }
}

2 Answers 2

5

If it is indeed working, the file will be at /data/data/your.package.name/. You will only be able to access that folder on an emulator unless your phone is rooted.

Sign up to request clarification or add additional context in comments.

4 Comments

and it's there anyway to copy the file to a more "public" folder?? so it can be reached by other applications... for example if i copy a picture, want to be display it by the Galery.. same with an Mp3..
For pictures there's a content provider you can put it into that's build into android. If you want to make it accessible to other applications, you either have to write your own content provider, or put it on the SD card
If you want to put it in a world accessible (allowing anyone to read or write it), put it in external storage: developer.android.com/reference/android/os/…
Does anyone can help me with the content provider for the IMAGES or give me link to an example to copy a file and make it available for other apps, using content providers??
0

if you want to save it in the - for users accesible - external storage, use this File f = new File(Environment.getExternalStorageDirectory(),"Folder/name.ending"); to create the file to copy, also use that path. If you don't want the image to appear in the Gallery of the phone, add a file called ".nomedia" to the same directory as the image

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.