I want to capture video and send it to the server in base64. Before sending it, I want to check the video length and video size. I am able to capture the video
switch (v.getId()) {
case R.id.camera_button:
intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, INTENT_VIDEO);
}
break;
}
}
and get the URI
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == INTENT_VIDEO && resultCode == RESULT_OK) {
Uri uri = data.getData();
}
}
I am able to get a video path
private String getPath(Uri video) {
String path = video.getPath();
String[] projection = {MediaStore.Video.Media.DATA};
Cursor cursor = getContentResolver().query(media, projection, null, null, null);
if (cursor.moveToFirst()) path = cursor.getString(cursor.getColumnIndexOrThrow(projection[0]));
cursor.close();
return path;
}
How do I get a video object from that path so that I could compress, check video duration, file size?
For image, it will be as simple as this
BitmapFactory.decodeFile(photoPath);
and after that, I would like to convert it to base64.
For image, it's as simple as this
private String toBase64(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outputStream);
byte[] bytes = outputStream.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
Currently I am doing like this
private String toBase64(Uri video) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
String path = getPath(video);
File tmpFile = new File(path);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile));
long length = tmpFile.length();
int inLength = (int) length;
byte[] b = new byte[inLength];
int bytesRead;
while ((bytesRead = in.read(b)) != -1) {
baos.write(b, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}
I get the base64, though I don't know if it's the correct one. but, I could not check video size, duration, etc.
you can compress video using ffmpeg. this is a sample project and this is the library.
Related
I am trying to first select PDF or docx file from memory then encode Base64 String and store in mysql database. When i click on upload button, it's not work. I think there was a problem in encode, but I'm unable to solve this problem. How to solve this problem or Is there any way to solve this issue??
Below is my code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
assert uri != null;
String uriString = uri.toString();
file = new File(uriString);
filepath = file.getAbsolutePath();
String displayName ;
if(uriString.startsWith("content://"))
{
Cursor cursor = null;
try {
cursor = this.getContentResolver().query(uri,null,null,null,null);
if(cursor!=null&&cursor.moveToFirst())
{
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Log.d("name",displayName);
}
}finally {
assert cursor != null;
cursor.close();
}
}else if(uriString.startsWith("file://"))
{
displayName = file.getName();
Log.d("name",displayName);
}
}
}
public static String convertFileToByteArray(File f) {
byte[] byteArray = null;
try {
InputStream inputStream = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024 * 11];
int bytesRead;
while ((bytesRead = inputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byteArray = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}
Indent in this way
public void getDoc() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
try {
InputStream iStream = getContentResolver().openInputStream(selectedImage);
byte[] inputData = getBytes(iStream);
long fileSizeInBytes = inputData.length;
long fileSizeInKB = fileSizeInBytes / 1024;
long fileSizeInMB = fileSizeInKB / 1024;
ParseFile resumes = new ParseFile("image.pdf",
inputData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
This question already has answers here:
data.getExtras().get("data") result of low resolution image in android
(2 answers)
Closed 4 years ago.
I integrated camera in my application and using that camera the captured image is blur. Any suggestions for improving captured image quality.
I am using multipart for sending image on server.
Code Snippet
#Override
public void openCameraAction() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
imageBase64 = encodeToBase64(photo);
imageUri = getImageUri(getApplicationContext(), photo);
imageFile = new File(getRealPathFromURI(imageUri));
getImageView.setImageBitmap(photo);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),imageFile);
MultipartBody.Part image = MultipartBody.Part.createFormData(imageQuestionId, imageFile.getName(),requestBody);
parts.add(image);
}
}
public static String encodeToBase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
Find a class image picker in this gist
https://gist.github.com/r00786/2c9aa88b706daccd55098ac2c28e7f39
all the things are handled in this class
How to use
private static final int PICK_IMAGE_ID = 234; // the number doesn't matter
public void onPickImage(View view) {
Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case PICK_IMAGE_ID:
Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
// TODO use bitmap
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
If you want the actual image taken camera to server u need to create
the image
Try this code
Delcare this Variable
private String actualPictureImagePath = "";
Then call this method on button click cameraIntent()
private void cameraIntent() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
actualPictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 1);
}
and Then in onActivityResult() handle this
#override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
File imgFile = new File(actualPictureImagePath);
if(imgFile.exists()){
InputStream inputStream = null;//You can get an inputStream using any IO API
inputStream = new FileInputStream(imgFile.getAbsolutePath());
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output64.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
output64.close();
String base64String = output.toString();
}
}
}
This is the code to use for Bitmap to Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm
is the bitmap object
byte[] byteArrayImage = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage,
Base64.DEFAULT);
NOTE:-
Do not forget to Add runtime permissions and in manifest also
1)Read and Write Persmission
2)Camera persmission
I am trying to copy image using below code:
Intent intentImage = new Intent();
intentImage.setType("image/*");
intentImage.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intentImage, 10);
With this i am able to open all image content.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10) {
if (resultCode != RESULT_OK) return;
Uri selectedImageUri = data.getData();
try {
String selectedImagePath1 = getPath(selectedImageUri);
File file = new File(selectedImagePath1);
String fna = file.getName();
String pna = file.getParent();
File fileImage = new File(pna, fna);
copyFileImage(fileImage, data.getData());
} catch (Exception e) {
}
}
}
private void copyFileImage(File src, Uri destUri) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(src));
bos = new BufferedOutputStream(getContentResolver().openOutputStream(destUri));
byte[] buf = new byte[1024];
bis.read(buf);
do {
bos.write(buf);
} while (bis.read(buf) != -1);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) bis.close();
if (bos != null) bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now i am successfully get path and name of the image .
Now when i run the above code then it gives me error of requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission().
so i have put the permission in manifest :
i have also defined the permission for read and write internal/external storage.
But still i am getting this error.
How can i copy image ?
Select picture using below code
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 1);
this will open gallery, after selecting pic you will get selected pic uri in below code
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case 1:
if(resultCode == RESULT_OK)
{
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
File sel = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(sel.getAbsolutePath());
imageView1.setImageBitmap(bitmap);
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600,370, true);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);
String StrBase64 = Base64.encodeToString(blob.toByteArray(), Base64.DEFAULT);
imageView1.setImageBitmap(resized);
// Toast.makeText(getApplicationContext(), ""+selectedImagePath, Toast.LENGTH_LONG).show();
}
break;
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
add permission in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
this way you will get selected image in Base64 to string
Try this code-
Image will copy in SaveImage folder in sd card
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case 1:
if(resultCode == RESULT_OK)
{
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
File sel = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(sel.getAbsolutePath());
imageView1.setImageBitmap(bitmap);
SaveImage(bitmap);
}
break;
}
}
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/SaveImage");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I use code below to make a picture with camera. Instead of saving I would like to encode it to Base64 and after that pass it to another API as an input. I can't see method, how to modify code to take pictures in Base64 instead of regular files.
public class CameraDemoActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
#Override
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);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
#Override
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");
}
}
}
I try to use code below to convert an image to Base64.
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);
}
Above described should be a much more direct and easier way than saving image and after that looking for image to encode it.
Try this:
ImageUri to Bitmap:
#Override
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 = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
String encodedImage = encodeImage(selectedImage);
}
}
Encode Bitmap in 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;
}
Encode from FilePath to 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;
}
output:
I've wrote my code like this :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Camera mCamera = Camera.open();
mCamera.startPreview();// 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() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("***************");
System.out.println(Base64.encodeToString(data, Base64.DEFAULT));
System.out.println("***************");
}
};
}
It works perfectly...
Just for converting from bitmap to base64 string in kotlin I use:
private fun encodeImage(bm: Bitmap): String? {
val baos = ByteArrayOutputStream()
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val b = baos.toByteArray()
return Base64.encodeToString(b, Base64.DEFAULT)
}
If you want your base64 String to follow the standard format, add this after getting your base64 method from any of the provided answers
String base64 =""; //Your encoded string
base64 = "data:image/"+getMimeType(context,profileUri)+";base64,"+base64;
The method to get imageExtension is
public static String getMimeType(Context context, Uri uri) {
String extension;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//If scheme is a content
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}
try {
val imageStream: InputStream? = requireActivity().getContentResolver().openInputStream(mProfileUri)
val selectedImage = BitmapFactory.decodeStream(imageStream)
val baos = ByteArrayOutputStream()
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val b = baos.toByteArray()
val encodedString: String = Base64.encodeToString(b,Base64.DEFAULT)
Log.d("check string" ,encodedString.toString())
} catch (e: IOException) {
e.printStackTrace()
}
For kotlin use code is given bellow just copy this and give image uri at "mProfileUri"
I would like the user to be able to import custom sounds in my app to change the default sounds. I already have this functionality working for bitmaps, but I would like to extend to sounds as well. The crucial step I am missing is audio decoding. I do not know what format the sound will come in, so I need to decode the audio before saving it to internal storage. For bitmaps this was accomplished by the bitmapfactory and the bitmap object, but I cannot find an analogous service for audio. This is the code that I have so far. The bitmap portions work, but the audio parts are incomplete.:
private void retrievepicture() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);//retrieve picture has a code of 1
}
private void retrievesound() {
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 2);//retrieve sound has a code of 2
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
usingintents = false;
if (requestCode == 1 && resultCode == Activity.RESULT_OK)
try {
InputStream stream = getContentResolver().openInputStream(
data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
stream.close();
File deletefile = new File(savepath);
System.out.println(String.format("Replacing file %s",deletefile.getPath()));
deletefile.delete();
saveImageToInternalStorage(bitmap,savepath);
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (requestCode == 2 && resultCode == Activity.RESULT_OK)
{
//The code for saving audio would go here
InputStream stream = getContentResolver().openInputStream(
data.getData());
}
super.onActivityResult(requestCode, resultCode, data);
}
public boolean saveImageToInternalStorage(Bitmap image, String filepath) {
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = new FileOutputStream(filepath);
// Writing the bitmap to the output stream
if(image.getWidth() >480 || image.getHeight() > 480)
image = Bitmap.createScaledBitmap(image, 480, 480, false);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
I got it to work by getting the file path from the intent, and copying the file as it is instead of decoding it. All the files that I looked at did not have a clear format(they weren't .wav or .mp3, there was no file type listed)
This is what my code looks like inside the onActivityResult for the sound portion
InputStream stream = getContentResolver().openInputStream(data.getData());
String FilePath = data.getData().getPath();
File original = new File(FilePath);
File deletefile = new File(savepath);
String newpath = deletefile.getParent() + "/" + original.getName();
deletefile.delete();
File newfile = new File(newpath);
FileOutputStream out = new FileOutputStream(newfile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.close();
stream.close();
Kotlin 1.2
fun saveAudio(myAudio:Intent?):String {//TURN ON PERSMISSIONS ON THE EMULATOR IN SETTINGS
//var bytes = ByteArrayOutputStream()
val audioURI = myAudio!!.data
try {
val audioDirectory = File(
(Environment.getExternalStorageDirectory()).toString() + AUDIO_DIRECTORY)
val out = File(audioDirectory, ("fullAudio" + ".mp3"))
if (!audioDirectory.exists()){
audioDirectory.mkdirs()
}
out.createNewFile()//cressh
val file = File(audioURI.path)
val fo = FileOutputStream(out)
//val input = FileInputStream(f)
var stream = contentResolver.openInputStream(audioURI)
var read = 0
fo.write(stream.readBytes(1024))
MediaScannerConnection.scanFile(this,
arrayOf(file.getPath()),
arrayOf("audio/mpeg"),null)
fo.close()
return file.absolutePath
}catch (e1: IOException){
e1.printStackTrace()
}
return ""
}