Tirair foto e conviewter paira Base64

Use o código abaixo paira fazer uma foto com a câmera. Em vez de save, eu gostairia de codificá-lo paira Base64 e, depois disso, passá-lo paira outra API como uma input. Não consigo view o método, como modificair o código paira tirair fotos no Base64 em vez dos files regulaires.

 public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } { public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } count ++; public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } } public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } { public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } } public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } } public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } }); public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } } public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } } public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } } public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) { } Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); stairtActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } } 

Eu tento usair o código abaixo paira conviewter uma image paira Base64 .

  • J2ME VS Android VS iPhone VS Symbian VS Windows CE
  • Como configurair programaticamente o maxLength no Android TextView?
  • GridView linhas mairgem externa
  • NoClassDefFoundError: resolução crashda de: Lokhttp3 / internal / Platform
  • O monodroid é mais lento?
  • Arquitetura Android / iOS Peer-To-Peer
  •  public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) { ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); image.compress(compressFormat, quality, byteArrayOS); return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT); } { public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) { ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); image.compress(compressFormat, quality, byteArrayOS); return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT); } 

    Acima descrita deve ser uma maneira muito mais direta e mais fácil do que poupair image e, depois, procurair image paira codificá-la.

  • Quando um Fragmento é substituído e colocado na stack traseira (ou removido), fica na memory?
  • Aplicativo nativo iOS / Android paira o Windows Phone
  • Visual Studio 2015 - Xamairin - Android - Obter "resource.id não contém uma definição paira xxx" quando tento fazer qualquer coisa no file .cs
  • Campos protegidos não visíveis paira subclasss
  • Posso usair javax.script no desenvolvimento do Android e, se sim, como?
  • Alterair programaticamente RelativeLayout toRightOf to to theftOf
  • 2 Solutions collect form web for “Tirair foto e conviewter paira Base64”

    Tente isso:
    ImageUri to Bitmap:

      @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { final Uri imageUri = data.getData(); final InputStream imageStream = getContentResolview().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); String encodedImage = encodeImage(selectedImage); } } }  @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { final Uri imageUri = data.getData(); final InputStream imageStream = getContentResolview().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); String encodedImage = encodeImage(selectedImage); } } 

    Codificair bitmap em base64

      private String encodeImage(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); return encImage; } {  private String encodeImage(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); return encImage; } 

    Codificair de FilePath paira base64

      private String encodeImage(String path) { File imagefile = new File(path); FileInputStream fis = null; try{ fis = new FileInputStream(imagefile); }catch(FileNotFoundException e){ e.printStackTrace(); } Bitmap bm = BitmapFactory.decodeStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); //Base64.de return encImage; } {  private String encodeImage(String path) { File imagefile = new File(path); FileInputStream fis = null; try{ fis = new FileInputStream(imagefile); }catch(FileNotFoundException e){ e.printStackTrace(); } Bitmap bm = BitmapFactory.decodeStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); //Base64.de return encImage; } }  private String encodeImage(String path) { File imagefile = new File(path); FileInputStream fis = null; try{ fis = new FileInputStream(imagefile); }catch(FileNotFoundException e){ e.printStackTrace(); } Bitmap bm = BitmapFactory.decodeStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); //Base64.de return encImage; } 

    saída: insira a descrição da imagem aqui

    Eu escrevi meu código como este:

     public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Camera mCamera = Camera.open(); mCamera.stairtPreview();// I don't know why I added that, // but without it doesn't work... :D mCamera.takePicture(null, null, mPicture); } private Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Oviewride public void onPictureTaken(byte[] data, Camera camera) { System.out.println("***************"); System.out.println(Base64.encodeToString(data, Base64.DEFAULT)); System.out.println("***************"); } }; } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Camera mCamera = Camera.open(); mCamera.stairtPreview();// I don't know why I added that, // but without it doesn't work... :D mCamera.takePicture(null, null, mPicture); } private Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Oviewride public void onPictureTaken(byte[] data, Camera camera) { System.out.println("***************"); System.out.println(Base64.encodeToString(data, Base64.DEFAULT)); System.out.println("***************"); } }; } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Camera mCamera = Camera.open(); mCamera.stairtPreview();// I don't know why I added that, // but without it doesn't work... :D mCamera.takePicture(null, null, mPicture); } private Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Oviewride public void onPictureTaken(byte[] data, Camera camera) { System.out.println("***************"); System.out.println(Base64.encodeToString(data, Base64.DEFAULT)); System.out.println("***************"); } }; } }; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Camera mCamera = Camera.open(); mCamera.stairtPreview();// I don't know why I added that, // but without it doesn't work... :D mCamera.takePicture(null, null, mPicture); } private Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Oviewride public void onPictureTaken(byte[] data, Camera camera) { System.out.println("***************"); System.out.println(Base64.encodeToString(data, Base64.DEFAULT)); System.out.println("***************"); } }; } 

    Funciona perfeitamente…

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.