2

I want to FTP an image from my Android app to my server. The problem is, I am trying to do this right now:

String data = client.storeFile("picture.png", myBitmap);

but you can't send bitmaps through storeFile, only files. So my question is: how can I put this bitmap into a file to send? Do I have to save the picture on the phone first? (I would prefer not to). Any ideas? Thanks.

        FTPClient client = new FTPClient();
        Bitmap myBitmap = my bitmap;

        try {
            client.connect("myhost");
            boolean login = client.login("un", "pw");
            client.enterLocalPassiveMode();
            client.setFileType(FTP.BINARY_FILE_TYPE);

            String data = client.storeFile("picture.png", myBitmap);
            //here is where I need a file, not bitmap

            logout = client.logout();
            client.disconnect();
user1282637
  • 1,827
  • 5
  • 27
  • 56

2 Answers2

1

Try to check this thread. You can change the context.getCacheDir() to the path that you want to store the bitmap file in the device. Is it what you're looking for? there's a file is created from a bitmap. To retreive the File entity, just use

File myFile = new File(filename);
Community
  • 1
  • 1
Harald Hoerwick
  • 132
  • 1
  • 1
  • 8
0

I found out that .storeFile takes in an InputStream, not a file. So what I had to do to send my bitmap was this:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
myBitmap.compress(CompressFormat.PNG,
        0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(
        bitmapdata);

FileInputStream in = new FileInputStream(new File(data));
boolean result2 = client.storeFile("serverpath", bs);
user1282637
  • 1,827
  • 5
  • 27
  • 56