0

so Im trying to display an image taken by the camera and want to send it via socket later on, but I noticed the onActivityResult() method isnt called and now causing further problems. Im using RESULT_OK, but checking for its value so its not negative. Also, in the manifest, I didnt restrict anything. So I (hope I) basically checked the common errors and still cant help myself.

MainActivity

 public class MainActivity extends AppCompatActivity {
 ImageView imageView;
 Socket clientSocket = null;
 byte[] byteFromImage;
 Bitmap bitmap;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                clientSocket = new Socket("192.168.178.41", 6066);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    final Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dispatchTakePictureIntent();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        //mb outputstream
           OutputStream output = clientSocket.getOutputStream();
                        output.write(byteFromImage);
                        output.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } }); }


   public void dispatchTakePictureIntent() {
 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, 1);
 }
  @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
        bitmap = imageBitmap;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        Bitmap copy = bitmap;
        //maybe jpg
        copy.compress(Bitmap.CompressFormat.PNG, 100, stream);

        byteFromImage = stream.toByteArray();
        imageView.setImageBitmap(imageBitmap);
    }
 }
 public void print(String message) {
     Log.e("My output: ",message);
 }

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.education.client">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera"
    android:required="true" />


<application
    android:noHistory="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

I appreciate any help!!

  • hope this may help you http://stackoverflow.com/questions/13304385/android-not-able-to-call-onactivityresult-method-after-capturing-photo-by-cam – Sandeep dhiman May 02 '17 at 02:28
  • i called setResult and now it calls the method after taking a picture. but after opening the camera, the main activity crashes because i get a nullpointer from byteFromImage at output.write(byteFromImage);. it doesnt make sense to me because that method is called after the onActivityResult() method –  May 02 '17 at 04:13
  • Its because your bitmap is not converted to byte array completely. – Sandeep dhiman May 02 '17 at 05:16
  • yes, it seems that bitmap is null because setImageBitmap is giving me a null pointer, what is wrong in retrieving it like that? –  May 02 '17 at 12:20

2 Answers2

1

I found my answer! I had to reinitialize the ImageView in onActivityResult() :) Thanks to @Sandeep dhiman !!

0

Use below code to get bitmap image from camera

  if(data.getData()==null){
    bitmap = (Bitmap)data.getExtras().get("data");
}else{
    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
}

and then convert this bitmap to byte array

Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22