3

I am looking to send an image from my android device to a computer / raspberry pi running python. I have already achieved a communication from my windows laptop (running a java client) using the following code.

BufferedImage image = ImageIO.read (new File("C:/Users/****/OneDrive/Pictures/Om-nom.png"));
s = new Socket("192.168.2.69",8000);
ImageIO.write(image, "png", s.getOutputStream());
s.close();

However, the BufferedImage library (java.awt.image) is not supported on android. How can I achieve something similar in android studio to run on my android device i.e. read a PNG file from the memory on my device into a byte buffer that can then be sent to a server PC.

Note: The location of my file would be similar to the following /storage/emulated/0/Downloads/frog-face_1f438.png

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56

2 Answers2

1

To obtain an image you can do this:

    // TODO check sdcard is available
    File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File photoPath = new File(directory, "temp.jpg");

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    final Bitmap bitmap = BitmapFactory.decodeFile(photoPath.getAbsolutePath(), options);

To send to your server you can use okHttp client or what kind of protocol you use to communicate with your server.

Update: In manifest you should point out this permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

If you run code on android 6 and above you should handle runtime permission (WRITE_EXTERNAL_STORAGE):

List of Android permissions normal permissions and dangerous permissions in API 23?

// oncreate method or whereever you want to call your code 
        if (ContextCompat.checkSelfPermission(this, 
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    STATUS_CODE);
        } else {
            // TODO run your code
        }


@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case STATUS_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // TODO run your code
            } else {
                // TODO show warning
            }
Gleichmut
  • 5,953
  • 5
  • 24
  • 32
0

Thanks for your help Gleichmut. I have managed to write some working code that is available on GitHub if anyone needs similar functionality in their app (sending images through TCP sockets).

GitHub Repository Here: https://github.com/Lime-Parallelogram/Sending-Images-Through-Sockets-Android-to-Python-

Note: My code on GitHub includes a python server and a java client designed to run on android - written in Android-Studio