无法将可拆分数组列表发送到服务类

时间:2019-05-26 09:48:22

标签: java android arraylist parcelable

我正在尝试将可包裹的arraylist传递给包含图像URL的服务。由于某种原因,所有数据类型都传递给服务,但不是可打包数组列表。 logcat中出现错误,原因是

  

E / JavaBinder:!!!绑定交易失败!!!

以下是我的imageurl Arraylist模型的代码。

public class ImagesUrl implements Parcelable {

    private int id;
    private String filename;
    private String imageurl ;

    public ImagesUrl(Integer id , String imageurl , String filename) {
        this.id = id;
        this.filename = filename;
        this.imageurl = imageurl ;
    }

    public static final Parcelable.Creator<ImagesUrl> CREATOR = new Parcelable.Creator<ImagesUrl>() {
        public ImagesUrl createFromParcel(Parcel in) {
            return new ImagesUrl(in.readInt(),in.readString(), in.readString());
        }

        public ImagesUrl[] newArray(int size) {
            return new ImagesUrl[size];
        }
    };

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getImageurl() {
        return imageurl;
    }

    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }

    public static Creator<ImagesUrl> getCREATOR() {
        return CREATOR;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(filename);
        dest.writeString(imageurl);
    }

    private void readFromParcel(Parcel in) {
        id = in.readInt();
        filename = in.readString();
        imageurl = in.readString();
    }


}

以下是启动服务的代码:

 Intent i= new Intent(SyncActivity.this,ImageDownloadService.class);
    Bundle b=new Bundle();
    b.putParcelableArrayList("imageurls", imagesUrlArrayList);
    b.putInt("int",12);
    i.putExtras(b);
    startService(i);

Foowing是Service类的代码:

public class ImageDownloadService extends Service {

    private ArrayList<ImagesUrl> imagesUrlArrayList = new ArrayList<>();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {

        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

    }
    @Override
    public void onStart(Intent intent, int startid) {

        if(intent != null && intent.getParcelableArrayListExtra("imageurls") != null){
            imagesUrlArrayList = intent.getParcelableArrayListExtra("imageurls");
        }
        int val = intent.getIntExtra("int",0);
        Log.d(TAG, "onStart: "+(String.valueOf(imagesUrlArrayList.size())));
        Log.d(TAG, "onStart: "+(String.valueOf(imagesUrlArrayList.size())));
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
    }

    class GetImages extends AsyncTask<String, String, String> {

        private Integer current;
        String jsonStr;

        @Override
        protected String doInBackground(String... args) {

            try {
                Log.d(TAG, "doInBackground: "+String.valueOf(imagesUrlArrayList.size()));
                for (int i = 0; i < imagesUrlArrayList.size(); i++) {
                    downloadimage(imagesUrlArrayList.get(i).getImageurl(),imagesUrlArrayList.get(i).getFilename());
                }
                return "Images downloaded successfully. Please refresh your screen";


            } catch (Exception er) {
                Log.d(TAG, er.getMessage());
                appendLog(TAG+" GetCategories "+ er.getMessage());
                return er.getMessage();
            }

        }


        protected void onPostExecute(String file_url) {
          //  Toast.makeText(mContext, file_url, Toast.LENGTH_LONG).show();

        }
    }
    private  void  downloadimage(String URL ,String filename){

        try {
            URL url = new URL(URL);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            /*Context wrapper = new ContextWrapper(mContext);*/
            /*File internalStorage = wrapper.getDir("freshImages",MODE_PRIVATE);*/
            String root = Environment.getExternalStorageDirectory().toString();
            File internalStorage = new File(root + "/fresh/freshImages");

            if (!internalStorage.exists()) {
                internalStorage.mkdirs();
            }

            File file = new File(internalStorage, filename);

            InputStream inputStream = new BufferedInputStream(url.openStream() , 40 );

            // Bitmap bm = BitmapFactory.decodeStream(inputStream);

            OutputStream fileOutput = new FileOutputStream(file);

            Bitmap bmp =createScaledBitmapFromStream(inputStream,100,100);

            bmp.compress(Bitmap.CompressFormat.PNG, 90, fileOutput);
            inputStream.close();
            fileOutput.close();
            /*OutputStream fileOutput = new FileOutputStream(file);

            byte[] buffer = new byte[8192];
            int bufferLength = 0;

            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.close();*/
            //    Toast.makeText(getApplicationContext(), "image downloased", Toast.LENGTH_SHORT).show();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected Bitmap createScaledBitmapFromStream( InputStream s, int minimumDesiredBitmapWidth, int minimumDesiredBitmapHeight ) {

        final BufferedInputStream is = new BufferedInputStream(s, 32*1024);
        try {
            final BitmapFactory.Options decodeBitmapOptions = new BitmapFactory.Options();
            // For further memory savings, you may want to consider using this option
            // decodeBitmapOptions.inPreferredConfig = Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel

            if( minimumDesiredBitmapWidth >0 && minimumDesiredBitmapHeight >0 ) {
                final BitmapFactory.Options decodeBoundsOptions = new BitmapFactory.Options();
                decodeBoundsOptions.inJustDecodeBounds = true;
                is.mark(32*1024); // 32k is probably overkill, but 8k is insufficient for some jpgs
                BitmapFactory.decodeStream(is,null,decodeBoundsOptions);
                is.reset();

                final int originalWidth = decodeBoundsOptions.outWidth;
                final int originalHeight = decodeBoundsOptions.outHeight;

                // inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
                decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / minimumDesiredBitmapWidth, originalHeight / minimumDesiredBitmapHeight));

            }

            return BitmapFactory.decodeStream(is,null,decodeBitmapOptions);

        } catch( IOException e ) {
            throw new RuntimeException(e); // this shouldn't happen
        } finally {
            try {
                is.close();
            } catch( IOException ignored ) {}
        }

    }

}

如果我在服务类中获得了可调整的数组列表,我将调用GetImages asyntask。

1 个答案:

答案 0 :(得分:0)

我的arraylist包含5000个无法通过intent发送的URL。我制作了一个util类,以在活动之间传递和接收数组。