Image Uri to Bytesarray

Image Uri to bytesarray

From Uri to get byte[] I do the following things,

InputStream iStream =   getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);

and the getBytes(InputStream) method is:

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();
}

onActivityResult() Uri to Byte array

After putting together some SO examples and google group codes, i'm able to acheive it by this code:

Uri data = result.getData();   
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(data.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();

How to convert image URI to Byte[](Byte array) in React Native

react-native-image-picker provides option for base64 string. Set includeBase64 prop to true. You will get base64 encoded string in response.

How to Convert byte[] to Uri Image

It depends on the contents of the array of bytes.

Let assume that your array of bytes are URI characters, you can use the following code:

byte [] buf = <your byte array>;
String s = new String(buf, "UTF-8");
Uri uri = Uri.parse(s);

But In your case it's not possible to convert image byte array to image Uri, U can save your image Uri instead of storing bitmap. so that u can read it as an image Uri and reduce memory space too.

How to get the byteArray of an image in gallery, inside the onActivityResult method?

Here is the onClickListener on the "Add Image" button

There are no crop extras in Android, let alone for ACTION_PICK.

Here is the onActivityMethod()

ACTION_PICK returns a Uri, as is covered in the documentation. It does not return any extras.

Pass that Uri to your favorite image-loading library (e.g., Glide, Picasso), so it can load that image on a background thread.

Or, fork your own background thread, and use that along with a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Pass that InputStream to BitmapFactory and its decodeStream() method. But, please, use an image-loading library.

how to pass an image Uri or alternatively a bitmap to int parameter of SQLite database to save an image?

Try with following code, you can add one more column in you table for imageUri and save imagepath.

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 100);
}
});

//The onActivityResult method will invoke after select image from gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch (requestCode){
case 100:
if(resultCode == RESULT_OK){
try {
final Uri imageUri = imageReturnedIntent.getData();
String imageUri = imageUri.toString();
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
}

Get image with given url and convert it to byte array

The Javadoc for File(URI) constructor specifies that the uri has to be a "File" URI. In other words, it should start with "file:"

uri An absolute, hierarchical URI with a scheme equal to "file", a
non-empty path component, and undefined authority, query, and fragment
components

But you can achieve what you are trying to do by using an URL, instead of a File/URI:

URL imageURL = new URL(profileImgUrl);
BufferedImage originalImage=ImageIO.read(imageURL);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos );

//Persist - in this case to a file

FileOutputStream fos = new FileOutputStream("outputImageName.jpg");
baos.writeTo(fos);
fos.close();


Related Topics



Leave a reply



Submit