1

I am working on an Android Application which needs to implement the Bluetooth functionality. It needs to receive an image from an external h/w device, and then save that byte array to SD card as an image file.

Here is my code:

public void run(){
                Log.i(TAG, "BEGIN mConnectedThread");
                byte[] buffer = new byte[4096];

            //CREATE IMAGE FILE
            File test=new File(Environment.getExternalStorageDirectory(), "test.jpg");

            if (test.exists()) {
                test.delete();
            }

            try {
                fos = new FileOutputStream(test.getPath());
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    /*mEmulatorView.write(buffer, bytes);
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();*/
                    //receivedData = new String(buffer);

                    receivedData=new String(buffer)

                        //WRITE BYTES TO FILE
                        try {

                            fos.write(buffer);

                        }
                        catch (java.io.IOException e) {
                            Log.e("PictureDemo", "Exception in photoCallback", e);
                        }

                        /*
                        Bitmap bitmap = null;
                        try {
                            bitmap = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
                        } catch (OutOfMemoryError e) {
                            e.printStackTrace();
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                        if(bitmap != null)

                                testModeOnScreen.saveBitmapToSdcardAndEmail(bytes, bitmap);
                         */}

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }

My problem is that I am not getting any exception or crashes, but I am getting a corrupted image file.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
Ricky Khatri
  • 952
  • 2
  • 16
  • 42

2 Answers2

1

There seem to be multiple issues here. For starters, garbage at the buffer tail, "Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected":

http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#read()

Also, you may happen to need to use the bitmap functionality from Android.

Look how they exchange bitmaps here:

http://kamrana.wordpress.com/2012/05/12/sending-images-over-bluetooth-in-android/

And how they save to a file:

Save bitmap to file function

http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

You could find it easier hacking together those building blocks than reinventing them.

Community
  • 1
  • 1
full.stack.ex
  • 1,747
  • 2
  • 11
  • 13
1

check this solution;)

String TAG="IMG";
        File path = Environment.getExternalStoragePublicDirectory(("/mnt/exSdCard/RecievedIMAGE"));
        File file = new File(path, "test.jpg");
        BufferedInputStream bufferedInputStream = null;

        try {
            // Make sure that this path existe if it is not so create it
            path.mkdirs();

            bufferedInputStream = new BufferedInputStream(inputStream);
            OutputStream os = new FileOutputStream(file);

            final byte[] buffer = new byte[2048];
            int available = 0;

            while ((available = bufferedInputStream.read(buffer)) >= 0) {
                os.write(buffer, 0, available);
            }
            bufferedInputStream.read(buffer);


            bufferedInputStream.close();
            os.close();
            Log.d(TAG, "success" + buffer);

        } catch (IOException e) {
            // Unable to create file, likely because external storage is
            // not currently mounted.
            Log.w("ExternalStorage", "Error writing " + file, e);
        }