将多个图像上传到服务器-Android

时间:2015-05-24 12:01:21

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

在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

2 个答案:

答案 0 :(得分: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了解详情。

答案 1 :(得分:0)

使用UniversalImageLoader Library消除OutOfMemoryError。

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

相关问题