My code what it does is take an image from the gallery and take its route in string
and waiting for it in the database awaits the route of the image and displays it.
I want to be able to take a photo from the camera and also choose from the gallery and be able to show the image and that the image becomes base64
and the result await it in the database, to be able to have the possibility of doing services with web being able to send a string
that is the result of the image in base64
if you do not explain it well, you can comment on it.
My code is as follows: Activity:
public class CreateBits extends AppCompatActivity {
EditText editText;
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
Button nxtActivity;
SharedPreferencesController spc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_bits);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
editText = (EditText)findViewById(R.id.edtTextNameBit);
spc = new SharedPreferencesController(this);
}
public void addBit(View view){
String bitName = editText.getText().toString();
App.addBits(bitName, imgDecodableString, spc.getCategoryPID());
finish();
}
public void Cancell(View view){
finish();
}
public void addImage(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
//Log.i("RegirstrarPerfil",data.toString());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
Log.i("Main", imgDecodableString);
ImageView imgView = (ImageView) findViewById(R.id.imgCreateBit);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
AddBits method:
public static Bits addBits(String bName, String bImage, long CID){
long bitId;
Log.i("App de Luis "," Bit guardado con CID "+CID);
Number bitNumber = realm.where(Bits.class).max("bitId");
if(bitNumber == null)
bitId = 1;
else
bitId = (long)bitNumber + 1;
realm.beginTransaction();
Bits newBit = realm.createObject(Bits.class);
newBit.setbitId(bitId);
newBit.setbText(bName);
newBit.setbImage(bImage);
newBit.setCID(CID);
realm.commitTransaction();
return newBit;
}