Hey guys I wanted to save new image uploaded by user into SQLite database and show all images uploaded in list view. The user first uploads an image it works fine, but subsequent uploaded images will update the previous images to the latest image. This cause all images to be the same as all previous images are updated to latest image).
What is the cause of this? The way I save and retrieve from SQLite is I'll convert Uri to String then save as TEXT in SQLite database, when I retrieve it I will convert String back to Uri. Below shows my code:
PictureItem.java
public class PictureItem {
Uri image;
public PictureItem(Uri image) {
this.image = image;
}
public Uri getImage() {
return image;
}
public void setImage() {
this.image = image;
}
}
PictureAdapter.java (ArrayAdapter for listview)
public class PictureAdapter extends ArrayAdapter {
LayoutInflater inflator;
private List list = new ArrayList();
private Context context;
public PictureAdapter(Context context, int resources) {
super(context, resources);
this.context = context;
}
public void add(PictureItem object) {
list.add(object);
super.add(object);
}
static class ImageHolder {
ImageView pic_image;
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder;
if(inflator == null) inflator = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
row = inflator.inflate(R.layout.row_feed, parent, false);
holder = new ImageHolder();
holder.pic_image = (ImageView) row.findViewById(R.id.pic_image);
row.setTag(holder);
}
else holder = (ImageHolder) row.getTag();
PictureItem picture = (PictureItem) getItem(position);
Picasso.with(context).load(picture.getImage()).into(holder.pic_image);
return row;
}
}
CreatePicture.java (Where image is uploaded by user and saved in SQLite)
public class CreatePicture extends ActionBarActivity implements View.OnClickListener{
ImageView picture;
String uriPicture = ""; // Save uri in string format
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
picture.setImageDrawable(null);
Crop.pickImage(this);
// Add information into database
TableDatabase tableDatabase = new TableDatabase(this);
tableDatabase.putInformation(uriPicture);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
beginCrop(data.getData());
} else if(requestCode == Crop.REQUEST_CROP) {
handleCrop(resultCode, data);
}
}
// This method allows users to crop image in square.
private void beginCrop(Uri source) {
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(source, destination).asSquare().start(this);
}
// This method ensures there are no errors in cropping.
private void handleCrop(int resultCode, Intent result) {
if(resultCode == RESULT_OK) {
// Convert Uri to String to save in SQLite as TEXT format
uriPicture = Crop.getOutput(result).toString();
} else if(resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
PictureFragment.java (Where image list view is displayed)
public class PictureFragment extends Fragment {
private ListView listView;
private String image;
private PictureAdapter adapter;
private TableDatabase tableDatabase;
private Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_picture, container, false);
// Set listview
listView = (ListView) rootView.findViewById(R.id.piclistView);
adapter = new PictureAdapter(getActivity().getApplicationContext(), R.layout.row_feed);
listView.setAdapter(adapter);
// Retrieve data from database
tableDatabase = new TableDatabase(getActivity());
cursor = tableDatabase.getInformation(tableDatabase);
// Start from the last so that listview displays latest image first
if(cursor.moveToLast()) {
do {
image = cursor.getString(0);
PictureItem pictureItem = new PictureItem(Uri.parse(image));
adapter.add(pictureItem);
} while (cursor.moveToPrevious());
}
return rootView;
}
Aucun commentaire:
Enregistrer un commentaire