Hello, I am creating an application in which the user uploads a photo from their phone to a Database so that other users can see it. I used SQL Server as a database, and the image that is uploaded to the database is converted into Base64.encodeToString
:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case COD_SELECCIONA:
final Uri miPath = data.getData();
String path = getRealPathFromURI(getApplicationContext(), miPath);
image.setImageURI(miPath);
try {
convertphoto(miPath,path);
} catch (URISyntaxException e) {
e.printStackTrace();
}
break;
case COD_FOTO:
MediaScannerConnection.scanFile(getActivity(), new String[]{this.path}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i("Ruta de almacenamiento", "Path: " + path);
}
});
//image.setImageBitmap(bitmap);
Bitmap bitmap = BitmapFactory.decodeFile(this.path);
rotateImage(bitmap,this.path);
Bitmap image = ((BitmapDrawable) this.image.getDrawable()).getBitmap();
width=image.getWidth();
height=image.getHeight();
Bitmap imageScaled = Bitmap.createScaledBitmap(image, width, height, false);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageScaled.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byteArray = byteArrayOutputStream.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
break;
}
}
}
private void convertphoto(Uri selectedImage, String path) throws URISyntaxException {
Bitmap originBitmap = null;
File myFile = new File(selectedImage.getPath());
FileInputStream imageStream=null;
try {
imageStream = (FileInputStream) getActivity().getContentResolver().openInputStream(selectedImage);
originBitmap = BitmapFactory.decodeStream(imageStream);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage().toString());
}
if (originBitmap != null) {
this.image.setImageBitmap(originBitmap);
Log.w("Image Setted in", "Done Loading Image");
try {
Bitmap image = ((BitmapDrawable) this.image.getDrawable()).getBitmap();
width=image.getWidth();//-500;
height=image.getHeight();//-500;
int temp;
if(width>height){
temp=width-height;
}else{
temp=height-width;
}
width -=temp;
height -=temp;
Bitmap imageScaled = Bitmap.createScaledBitmap(image, width, height, false);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageScaled.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byteArray = byteArrayOutputStream.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (Exception e) {
Log.w("OOooooooooo", "exception");
}
}
}
When uploading the photo to the database it takes time to upload it. After having uploaded it, it is saved in a column and a very large amount of String
. When I want to read that image it takes a lot of time to read it because of the String that it contains.
This is the table:
This is the code to read the image:
@Override
protected void onPostExecute(String resultSet) {
//Stoping the progress bar and showing the message
progressBar.setVisibility(View.GONE);
//Checking if image we got is empty or we have success
if (resultSet.matches("")) {
} else {
resultSet.toString();
//Decoding and Setting Image in the imageview
byte[] decodeString = Base64.decode(resultSet, Base64.DEFAULT);
Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length);
Bitmap image = decodebitmap;
Bitmap imageScaled = Bitmap.createScaledBitmap(image, image.getWidth(), image.getHeight() , false);
imageView.setImageBitmap(resizeImage(imageScaled, width , height ));
}
}
}
I would like to know how I can do to read the images saved in SQL Server much faster.