我已经搜索但是找到了一些答案,但可能是我的错,找不到我想要的答案。现在我正在尝试下面:
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。所以我想在上传图片时检查它是否有交叉限制。
2.
我想检查图像是否是png格式。如果不是那么就不会上传。显示Toast。可以转换图像吗? :(
3.
如果用户正在上传假设700kbyte的图像,但是配置文件pic很小,即100kbyte足够用于配置文件pic,那么我可以将pic压缩到我定义的大小,然后将其上传到数据存储区。如果用户可以保持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
。当我上传图片时需要时间。所以我应该在将图像转换为所需尺寸后上传时使用asynctask吗?
2
。当我第一次进入我的应用程序时,我显示了个人资料图片,即如果我登录我的帐户,它将从数据存储区获取配置文件的图像。但这需要很长时间,我的登录似乎很长
答案 0 :(得分:1)
我通过缩小图像解决了我的问题。 这是我的代码:
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;
}
}