gpt4 book ai didi

java - 无法将可打包数组列表发送到服务类

转载 作者:行者123 更新时间:2023-12-02 05:13:39 25 4
gpt4 key购买 nike

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

E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! .

以下是我的 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。

最佳答案

我的 arraylist 有 5000 个 url,无法通过 Intent 发送。我创建了一个 util 类来在 Activity 之间传递和接收数组。

关于java - 无法将可打包数组列表发送到服务类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com