在 android 中面临图像上传和检索到数据存储的困难?
Facing Image Uploading and retrieving difficulties to Datastore in android?
我已经搜索了一些答案,但可能是我的错,找不到我想要的answer.Now低于我正在尝试的答案:
I am trying to upload an image like of a status or a post or any profile pic.Profile pic will be small and status or any post image will be big
。现在我想做什么:
1.
我正在将图像转换为字符串文本并将其上传到数据存储,它的限制是 1Mbyte.So 我想在上传图像时检查它是否超过限制。
2.
我想检查图像是否为 png format.If 它不是然后不会 upload.Show Toast.Can 我在那里转换图像? :(
3.
如果用户正在上传假设 700kbyte 的图片,但个人资料图片很小,即 100kbyte 就足够用于个人资料图片,那么我可以将图片压缩到我定义的大小,然后将其上传到 datastore.It 可能会保持 700kbyte如果是状态图。
我正在将图像转换为字符串并将其上传到数据存储区,然后在我的应用程序中显示它时再次转换回图像。My code
:
public static String imageToStringConverter(Bitmap image){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return imageToString;
}
public static Bitmap stringToimageConverter(String imageString){
byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
return bmp;
}
现在我面临的问题
:
1
。当我上传正在拍摄的图片时 time.So 我应该在将图片转换为我想要的大小后在上传时使用 asynctask 吗??
2
。当我第一次进入我的应用程序时,我显示了个人资料图片,即如果我登录我的帐户,它将从 datastore.But 获取个人资料图片,这需要很多时间而且我的登录似乎很长。
我已经通过缩小图像解决了我的问题。
这是我的代码:
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
Uri imageUri;
try {
imageUri = imageReturnedIntent.getData();
}catch(Exception e){
Toast.makeText(getActivity(),"Image Not Found",Toast.LENGTH_SHORT).show();
return;
}
//final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
//final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
ShrinkBitmapConverter sh = new ShrinkBitmapConverter(getActivity());
Bitmap selectedImage = null;
try {
selectedImage = sh.shrinkBitmap(imageUri,450,350);
} catch (Exception e) {
Toast.makeText(getActivity(),"Image Not Found",Toast.LENGTH_SHORT).show();
}
statusImage = ImageConverter.imageToStringConverter(selectedImage);
if(statusImage.length()>512000){
Toast.makeText(getActivity(),"Image is too big",Toast.LENGTH_LONG).show();
}else {
postImage.setImageBitmap(selectedImage);
}
}
}
ImageConverter.java:
public class ImageConverter {
public static String imageToStringConverter(Bitmap image){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return imageToString;
}
public static Bitmap stringToimageConverter(String imageString){
byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
return bmp;
}
}
ShrinkBitmapConverter.java:
public class ShrinkBitmapConverter {
Context context;
public ShrinkBitmapConverter(Context c){
context=c;
}
public Bitmap shrinkBitmap(Uri uri,int width,int height) throws FileNotFoundException {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = null;;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,bmpFactoryOptions);
} catch (Exception e) {
Toast.makeText(context,"Image Not Found",Toast.LENGTH_SHORT).show();
}
return bitmap;
}
}
我已经搜索了一些答案,但可能是我的错,找不到我想要的answer.Now低于我正在尝试的答案:
I am trying to upload an image like of a status or a post or any profile pic.Profile pic will be small and status or any post image will be big
。现在我想做什么:
1.
我正在将图像转换为字符串文本并将其上传到数据存储,它的限制是 1Mbyte.So 我想在上传图像时检查它是否超过限制。
2.
我想检查图像是否为 png format.If 它不是然后不会 upload.Show Toast.Can 我在那里转换图像? :(
3.
如果用户正在上传假设 700kbyte 的图片,但个人资料图片很小,即 100kbyte 就足够用于个人资料图片,那么我可以将图片压缩到我定义的大小,然后将其上传到 datastore.It 可能会保持 700kbyte如果是状态图。
我正在将图像转换为字符串并将其上传到数据存储区,然后在我的应用程序中显示它时再次转换回图像。My code
:
public static String imageToStringConverter(Bitmap image){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return imageToString;
}
public static Bitmap stringToimageConverter(String imageString){
byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
return bmp;
}
现在我面临的问题
:
1
。当我上传正在拍摄的图片时 time.So 我应该在将图片转换为我想要的大小后在上传时使用 asynctask 吗??
2
。当我第一次进入我的应用程序时,我显示了个人资料图片,即如果我登录我的帐户,它将从 datastore.But 获取个人资料图片,这需要很多时间而且我的登录似乎很长。
我已经通过缩小图像解决了我的问题。 这是我的代码:
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
Uri imageUri;
try {
imageUri = imageReturnedIntent.getData();
}catch(Exception e){
Toast.makeText(getActivity(),"Image Not Found",Toast.LENGTH_SHORT).show();
return;
}
//final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
//final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
ShrinkBitmapConverter sh = new ShrinkBitmapConverter(getActivity());
Bitmap selectedImage = null;
try {
selectedImage = sh.shrinkBitmap(imageUri,450,350);
} catch (Exception e) {
Toast.makeText(getActivity(),"Image Not Found",Toast.LENGTH_SHORT).show();
}
statusImage = ImageConverter.imageToStringConverter(selectedImage);
if(statusImage.length()>512000){
Toast.makeText(getActivity(),"Image is too big",Toast.LENGTH_LONG).show();
}else {
postImage.setImageBitmap(selectedImage);
}
}
}
ImageConverter.java:
public class ImageConverter {
public static String imageToStringConverter(Bitmap image){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return imageToString;
}
public static Bitmap stringToimageConverter(String imageString){
byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
return bmp;
}
}
ShrinkBitmapConverter.java:
public class ShrinkBitmapConverter {
Context context;
public ShrinkBitmapConverter(Context c){
context=c;
}
public Bitmap shrinkBitmap(Uri uri,int width,int height) throws FileNotFoundException {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = null;;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,bmpFactoryOptions);
} catch (Exception e) {
Toast.makeText(context,"Image Not Found",Toast.LENGTH_SHORT).show();
}
return bitmap;
}
}