Skip to content

Instantly share code, notes, and snippets.

@VictorKoenders
Created August 4, 2016 14:14
Show Gist options
  • Save VictorKoenders/dfc62842d4024c3d22cbb75ccd870ed8 to your computer and use it in GitHub Desktop.
Save VictorKoenders/dfc62842d4024c3d22cbb75ccd870ed8 to your computer and use it in GitHub Desktop.
package iproductive.hortimax.com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView _textView;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static MainActivity _instance;
private static boolean _hasResults = false;
private static String _resultUrl = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
_instance = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_textView = (TextView) findViewById(R.id.TextView);
_textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
final String photo = MainActivity.takePhoto();
_instance.runOnUiThread(new Runnable(){
@Override
public void run() {
_instance._textView.append("\n" + photo);
}
});
}
}).start();
}
});
}
public static String takePhoto(){
_hasResults = false;
_resultUrl = "";
_instance.runOnUiThread(new Runnable() {
@Override
public void run() {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture Intent
_instance.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
while(!_hasResults){
try {
Thread.sleep(100, 0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return _resultUrl;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Log.d("Photo", "Result code: " + resultCode);
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
_resultUrl = data.getData().toString();
Log.d("Photo", "Result url: " + _resultUrl);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
_resultUrl = "";
} else {
// Image capture failed, advise user
Toast.makeText(_instance, "Could not take picture", Toast.LENGTH_LONG);
_resultUrl = "";
}
_hasResults = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment