Я пытаюсь передать пакетного архива в службу, содержащую 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);
Folowing - код для класса обслуживания:
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, если получу ускоренный массив в классе обслуживания.