【问题标题】:Upload Multiple Images To Server -Android将多个图像上传到服务器 -Android
【发布时间】:2015-05-24 12:01:21
【问题描述】:

在 bello 示例中,我将数组作为附加对象,我创建了一个数组 imagePath[]

它存储来自gridview的所有选定图像的路径,但我的问题是当我这样做时

它用于gridview中的第一张图像然后它工作正常但是当我选择另一个图像或

public class ShowImages extends Activity {
String title1[],title[],imagePath[];
  Bitmap[] mResources;
                    int cnt=0,j=0;
                    ImageView iv;
                      @Override
                        public void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.layoutxml);
                            iv=(ImageView)findViewById(R.id.imageView1);
                            Bundle extras = getIntent().getExtras();
                            title1 = getIntent().getExtras().getStringArray("imageArray");
                            for(int i=0;i<title1.length;i++){
                                if(title1[i] != null){
                                    cnt++;
                                }
                            }

                            imagePath=new String[cnt];
                            for(int i=0;i<title1.length;i++){
                                if(title1[i] != null){
                                    if(j<cnt){
                                    imagePath[j]=title1[i];
                                    j++;
                                    }
                                }
                            }
                       //  Toast.makeText(getApplicationContext(),String.valueOf(cnt), Toast.LENGTH_LONG).show();
                        mResources=new Bitmap[cnt];     

                        try{
                         for(int i=0;i<cnt;i++)
                         {

                             Toast.makeText(getApplicationContext(),imagePath[i], Toast.LENGTH_LONG).show();
                             File imgFile = new  File(imagePath[i]);
                             if(imgFile.exists())
                             {
                                 mResources[i] = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                                // iv.setImageBitmap(mResources[i]);
                             }
                        }
                        }
                        catch(Exception e){

                        }
                      } 

                }

gridview 中的多个图像它在 logcat 中给出以下错误

 05-24 17:10:12.616: E/art(3371): Throwing OutOfMemoryError "Failed to    allocate a 63701004 byte allocation with 524288 free bytes and 46MB until OOM"
 05-24 17:10:12.635: E/AndroidRuntime(3371): FATAL EXCEPTION: main
 05-24 17:10:12.635: E/AndroidRuntime(3371): Process: com.customgallery, PID: 3371
 05-24 17:10:12.635: E/AndroidRuntime(3371): java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 524288 free bytes and 46MB until OOM

【问题讨论】:

  • 第一个也是重要的问题,你为什么要创建位图数组?有关我的问题的更多信息,请查看以下问题:Strange out of memory issue while loading an image to a Bitmap object
  • @PareshMayani 我必须这样做,因为在 php 页面中我必须发送 base64 字符串 ..所以我需要将位图数组转换为 base64 字符串数组
  • 但是创建位图数组会导致同样的问题!而是将选择的图像 URI/URL 存储到字符串数组中,并在上传到服务器时将其转换为位图或 Base64!
  • @PareshMayani 如果可以在服务器上转换图像 int Base64 的路径(我的意思是 php 页面)你能给我提供代码
  • 这是一个菜鸟问题,您的服务器如何能够通过图像 URI 从您的设备获取实际图像?

标签: android arrays android-intent android-imageview android-bitmap


【解决方案1】:

使用缩小版的图像,这样可以避免内存和执行时间的浪费。

public static Bitmap decodeFile(String photoPath){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, options);

        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inPreferQualityOverSpeed = true;
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        return BitmapFactory.decodeFile(photoPath, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

访问http://developer.android.com/training/displaying-bitmaps/index.html了解更多信息。

【讨论】:

    【解决方案2】:

    使用 UniversalImageLoader 库消除 OutOfMemoryError。

    参考此链接https://github.com/nostra13/Android-Universal-Image-Loader

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2014-03-21
      • 2014-03-24
      • 2015-07-12
      • 2011-02-02
      • 2015-03-29
      • 2014-04-19
      • 2011-10-15
      • 2020-11-19
      相关资源
      最近更新 更多