【问题标题】:Facing Image Uploading and retrieving difficulties to Datastore in android?面临图像上传和检索到 Android 中的 Datastore 的困难?
【发布时间】:2015-03-07 17:31:15
【问题描述】:

我已经搜索了一些答案,但可能是我的错找不到我想要的答案。现在下面我正在尝试:
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 的图像,但配置文件图片很小,即 100kbyte 足以用于配置文件图片,那么我可以将图片压缩到我定义的大小,然后将其上传到数据存储。如果是状态图像,它可能会保持 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.When我第一次进入我的应用程序,我已经显示了个人资料图片,即如果我登录我的帐户,它将从数据存储中获取个人资料的图像。但这需要很多时间,而且我的登录似乎很长。

【问题讨论】:

  • 只保存数据存储中图像的 URL,并使用 get_serving_url 生成这些 URL。 cloud.google.com/appengine/docs/python/images/functions 然后图像将从 Google 的图像服务基础架构而不是您的应用提供。
  • 它不是免费的可能是:(。我使用的是免费版本。
  • 上传前不能调整图片大小吗??
  • 你可以在上传之前做任何你想做的事,这是你的应用程序!您还可以从 serving_url 以特定大小提供图像,请查看文档。
  • 我相信你可以不用付费就可以使用它。

标签: android image google-app-engine image-uploading image-conversion


【解决方案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;
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 2016-01-17
    • 2014-12-27
    • 1970-01-01
    • 2021-09-07
    • 2011-03-22
    • 2016-10-25
    • 2013-05-06
    相关资源
    最近更新 更多