java - 无法将 parcelable arraylist 发送到服务类
问题描述
我正在尝试将一个可打包的数组列表传递给包含图像 url 的服务。出于某种原因,所有数据类型都传递给服务,但不是 parcelable arraylist。一个错误logcat
即将到来
E/JavaBinder:!!!Binder 交易失败!!!.
以下是我的 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);
以下是服务类的代码:
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。
解决方案
我的数组列表有 5000 个无法通过意图发送的 url。我制作了一个 util 类来在活动之间传递和接收数组。
推荐阅读
- mongodb - (MongoDB Compass) 在一个输出中查找、项目和组
- scala - Jacoco 插件显示 Scala 单元测试的 0% 覆盖率报告
- python - Pandas 将每日交易量重新采样为每月,每个 DF 条目的日期范围
- android - 删除操作栏后,它会留下一个空白区域
- vb.net - VB.net 视频文件仅在 windows 媒体播放器中播放音乐
- numpy - Numpy 3D Array/Eigen Tensor C++,将每一行乘以其转置
- javascript - jQuery datetimepicker 上的递归错误太多
- scikit-learn - 关于 Anaconda 商业版 2.1.0 上的 scikit-learn 的问题
- android - Android CameraX:ImageCapture 与 Preview 有点不同
- angular - 提供预验证角度 Slick-grid 可拖动分组功能。还以编程方式从分组中删除特定列