我在A类中有一个名为Arraylist<image>
的图像数组。
我有一个异步任务,它是一个独立的文件。在异步任务中,我填充了一个Arraylist<String> imagePaths
,它是通过以下方式在doInBackground
中派生的:
for (Image image: images) {
imagePaths.add(getPath(image));
}
我想把imagePaths
发送到A
类,在后台完成后。我想在不传递A
类实例到AsyncTask的情况下完成它。 这是我的代码,但我无法从AsyncTask获取imagePath
:
public class SellCarInformationActivity extends BaseActivity implements
OnWheelChangedListener,
saveImageAsyncTaskListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Res.init(this);
setContentView(R.layout.activity_sell_car_information);
bimap = BitmapFactory.decodeResource(
getResources(),
R.drawable.icon_addpic_unfocused);
PublicWay.activityList.add(this);
parentView = getLayoutInflater().inflate(R.layout.activity_sell_car_information, null);
setContentView(parentView);
//hide editext key board when click other place
mHideEditor=new HideEditorKeyboard(this);
mHideEditor.setupUI(findViewById(R.id.sellCarInfromationId));
carInformationDB=new CarInformationDB(this);
carPictureUrlDB=new CarPictureUrlDB(this);
Init();
eventListener=this;
}
});
mCarPublishBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCarInformation=new CarInformation();
mCarInformation.setCarBrand(mBrandText.getText().toString());
mCarInformation.setCarModel(mModelText.getText().toString());
mCarInformation.setCarUsedHours(Integer.parseInt(mCarUsedHours.getText().toString()));
mCarInformation.setCarSite(mSiteText.getText().toString());
mCarInformation.setCarProducedYear(mCarProduceDateText.getText().toString());
mCarInformation.setCarPrice(Double.parseDouble(mCarPrice.getText().toString()));
mCarInformation.setCarUsedState(mCarStateText.getText().toString());
mCarInformation.setCarUsedPurpose(mCarUsingPurposeText.getText().toString());
mCarInformation.setCarUserDescriber(mCarDescriber.getText().toString());
mCarInformation.setCarUserName(mCarUserName.getText().toString());
mCarInformation.setCarUserPhone(mCarUserPhone.getText().toString());
//ArrayList<Bitmap> selectedPictures;
HashMap<String,Bitmap> nameAndPictures=new HashMap<String, Bitmap>();
String pictureName;
long j=0;
for(int i=0;i<Bimp.tempSelectBitmap.size();i++){
nameAndPictures.clear();
Bitmap selectedPicture= Bimp.tempSelectBitmap.get(i).getBitmap();
//selectedPictures=new ArrayList<Bitmap>();
//selectedPictures.add(selectedPicture);
j=System.currentTimeMillis();
j++;
pictureName=mModelText.getText().toString()+j;
nameAndPictures.put(pictureName,selectedPicture);
mCarInformation.setCarPictureLocalName(pictureName);
}
long carID=carInformationDB.insertCarInformation(mCarInformation);
mCarInformation.setCarId((int) carID);
// SaveImageToMemory saveImageToMemory=new SaveImageToMemory(selectedPicture,pictureName,mCarInformation);
SaveImageToMemory saveImageToMemory=new SaveImageToMemory(nameAndPictures,mCarInformation);
saveImageToMemory.setEventListener(eventListener);
saveImageToMemory.execute();
storeImagePathstoDB(carID);
Toast.makeText(SellCarInformationActivity.this,
"发布成功"+carInformationDB.getCarInformationByCarId(carID).getCarBrand()+
carInformationDB.getCarInformationByCarId(carID).getCarModel()
, Toast.LENGTH_SHORT).show();
}
});
}
public void storeImagePathstoDB(long caID) {
long carID =caID ;
userLoadPictureUrl=new UserLoadPictureUrl();
userLoadPictureUrl.setCarId((int) carID);
userLoadPictureUrl.setPictureUrl1(imagePaths.get(0));
userLoadPictureUrl.setPictureUrl2(imagePaths.get(1));
userLoadPictureUrl.setPictureUrl3(imagePaths.get(2));
userLoadPictureUrl.setPictureUrl4(imagePaths.get(3));
userLoadPictureUrl.setPictureUrl5(imagePaths.get(4));
userLoadPictureUrl.setPictureUrl6(imagePaths.get(5));
userLoadPictureUrl.setPictureUrl7(imagePaths.get(6));
userLoadPictureUrl.setPictureUrl8(imagePaths.get(7));
userLoadPictureUrl.setPictureUrl9(imagePaths.get(8));
carPictureUrlDB.insertUserLoadPictureUrl(userLoadPictureUrl);
carPictureUrlDB.getUserLoadPictureUrlByCarId(carID);
}
public class SaveImageToMemory extends AsyncTask< HashMap<String,Bitmap>, Void, ArrayList<String> > {
Bitmap image;
String imageName;
String imagePath;
CarInformation carInformation;
HashMap<String,Bitmap> nameAndPicture;
private Context context = null;
private ArrayList<String> storePictureUrl;
private saveImageAsyncTaskListener eventListener;
private ArrayList<String> imagePaths;
public SaveImageToMemory(HashMap<String,Bitmap> nameAndPicture, CarInformation carInformation) {
super();
// this.image = image;
// this.imageName = imageName;
this.carInformation = carInformation;
this.nameAndPicture=nameAndPicture;
}
@Override
protected ArrayList<String> doInBackground(HashMap<String,Bitmap>... params) {
HashMap<String,Bitmap> nameAndPicture=params[0];
imagePaths = new ArrayList<String>();
storePictureUrl= new ArrayList<>();
for(Map.Entry<String,Bitmap> entry :nameAndPicture.entrySet()){
imageName=entry.getKey();
image=entry.getValue();
imagePath = saveImageInternalMemory(image, imageName);
imagePaths.add(imagePath+ "/" +imageName);
}
return imagePaths;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
Log.d("News reader", "Feed downloaded");
eventListener.onImagePathsReady(imagePaths);
// carInformation.setCarPictureLocalUrl(imagePath);
// saveToDatabase(carInformation, imagePath);
}
public String saveImageInternalMemory (Bitmap bitmapImage, String imageName){
ContextWrapper cw = new ContextWrapper(AppController.getInstance().getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory, imageName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
public void setEventListener(saveImageAsyncTaskListener listener){
this.eventListener=listener;
}
}