I'm currently building an android app to apply filter on a bmp. I'm using the gpuimage lib. How it's done is that the bmp is show in a ListView which contain 8 filters. When scrolling down/up, we request the filtering of the bmp (b&w, sepia...). As the rendering take times, I display in my listview the original bmp and it's replace by the filtered image once done
Here is how the activity do it.
private ListView mFiltersView;
private void FiltersPreview(final Bitmap mBmp) {
boolean mPreview = true;
mPreviewBitmap = resizeBitmap(mBmp);
mCameraImageFiltersAdapter = new CameraImageFiltersAdapter(this, mPreviewBitmap, mPreview);
mFiltersView.setAdapter(mCameraImageFiltersAdapter);
mFiltersView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
mCameraImageFiltersAdapter.cancel();
mFiltersView.cancelPendingInputEvents();
mFiltersView.setAdapter(null);
final CameraFiltersFactory effect = (CameraFiltersFactory) v.findViewById(R.id.filteredImage).getTag();
BufferAnimationDrawable Loading = new BufferAnimationDrawable(getBaseContext());
Loading.setPrimaryColor(0xfffb633e);
LoadingScreen.setImageDrawable(Loading);
LoadingScreen.setVisibility(View.VISIBLE);
mFiltersView.setVisibility(View.GONE);
getActionBar().hide();
if(mBmp == null) Log.d(TAG,"mBitmap is null");
effect.save(mBmp, position, new GPUImage.OnPictureSavedListener() {
#Override
public void onPictureSaved(Uri uri) {
final Intent previewIntent = new Intent(FiltersSelectionActivity.this, PicturePreviewActivity.class);
previewIntent.setData(uri);
previewIntent.setAction(mActionTypes.toString());
previewIntent.putExtra("Type", "Filtered");
startActivityForResult(previewIntent, 0);
}
});
}
});
}
The mCameraImageFiltersAdapter is defined as :
public CameraImageFiltersAdapter(/*Activity activity, */Context c, Bitmap current, boolean isPreview) {
mContext = c;
mPreview = isPreview;
mCurrentBitmap = current;
mFilterIds = CAMERA_IMAGE_FILTERS == null
|| CAMERA_IMAGE_FILTERS.length == 0 ?
mFilterIds : CAMERA_IMAGE_FILTERS;
mFakeBitmap = mCurrentBitmap;
mFakeBitmap.setDensity(0);
mExecutorService = Executors.newFixedThreadPool(5);
}
private final Handler handler = new Handler();// handler to display images
public int getCount() { return mFilterIds.length; }
public long getItemId(int position) { return 0; }
public Object getItem(int position) { return null; }
#Override public int getViewTypeCount() { return mFilterIds.length; }
#Override public int getItemViewType(int position) { return position; }
final int stub_id = R.drawable.filter_preview_stub;
public ImageView filteredImage = null;
public TextView filteredText = null;
#SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
mPosition = position;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_filter, null);
filteredImage = (ImageView) convertView.findViewById(R.id.filteredImage);
filteredImage.setImageBitmap(mFakeBitmap);
filteredText = (TextView) convertView.findViewById(R.id.textview);
queueFiltered(filteredImage, mPosition, filteredText);
}
return convertView;
}
private void queueFiltered(final ImageView view, final int position, final TextView text) {
final CameraFiltersFactory holder = new CameraFiltersFactory(mContext, view, text);
if(holder != null)
mExecutorService.submit(new FilterLoader(holder, position));
}
public void cancel() {
if(mExecutorService != null) mExecutorService.shutdownNow();
}
The CameraFilterFactoy is just a easy to use class to access to the GPUImage
public class CameraFiltersFactory {
private static final String TAG = CameraFiltersFactory.class.getSimpleName();
private final ImageView mImageView;
private final GPUImage mCameraImage;
private Bitmap mFilteredBitmap;
private int mCurrentEffect;
private Context mContext;
private Activity mActivity = null;
private TextView mFiltersText;
public CameraFiltersFactory(Context c, ImageView filteredImage, TextView filteredText) {
mImageView = filteredImage;
mImageView.setTag(this);
mContext = c;
mCameraImage = new GPUImage(mContext);
if(filteredText != null) {
mFiltersText = filteredText;
mFiltersText.setVisibility(View.VISIBLE);
}
if(mImageView != null) mActivity = (Activity) mContext;
}
public void generateFilteredBitmap(Bitmap bmp, int filtertype, boolean isPreview) {
mCurrentEffect = filtertype;
switch (mCurrentEffect) {
case R.id.blackandwhite:
mCameraImage.setFilter(new GPUImagePlusGrayscaleFilter(isPreview));
break;
case R.id.cool:
mCameraImage.setFilter(new GPUImagePlusCoolFilter(isPreview));
break;
case R.id.cool2:
mCameraImage.setFilter(new GPUImagePlusCool2Filter(isPreview));
break;
case R.id.faded:
mCameraImage.setFilter(new GPUImagePlusFadedFilter(mContext, isPreview));
break;
case R.id.hipster:
mCameraImage.setFilter(new GPUImagePlusHipsterFilter(mContext, isPreview));
break;
case R.id.sepia:
mCameraImage.setFilter(new GPUImagePlusSepiaFilter(isPreview));
break;
case R.id.vivid:
mCameraImage.setFilter(new GPUImagePlusVividFilter(isPreview));
break;
case R.id.warm:
mCameraImage.setFilter(new GPUImagePlusWarmFilter(mContext, isPreview));
break;
default:
Log.d("NONE", "None FAIT CHIER");
break;
}
mCameraImage.deleteImage();
mCameraImage.setImage(bmp);
mFilteredBitmap = mCameraImage.getBitmapWithFilterApplied();
}
#SuppressLint("SimpleDateFormat")
public void save(Bitmap bitmap, int filter_id, GPUImage.OnPictureSavedListener ofsl) {
Log.d("NONE", "Save request with filter: "+filter_id);
generateFilteredBitmap(bitmap, filter_id, false);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = timeStamp + ".jpg";
mCameraImage.saveToPictures(mFilteredBitmap, CameraSettings.CAMERA_ROLL_FOLDER, fileName, true, ofsl);
}
}
This code is working fine in the List view.
Once I click on a picture from the ListView, I get his position, stop Executor from the adapter and ask to the FilterFactory for a rendering.
If In the listview I wait that all the preview list image are showing the filter rendering, and then I click, the filter is correctly applied on the original bmp.
In case, I'm scrolling quickly down and the GPU is in progress to render the preview iamge and then click, the original bmp is not filtered. I have check that in both case, when I click the list view give the right filter position and that the case. It seems that if a rendering is in progress, I'm not able to cancel and ask for a new one.
Any idea why ? Any idea if I can cancel the current GPU rendering and start a new one. ?
Thanks
Use gpuImage.deleteImage(); method in your adapter class after getting the bitmap from the gpu image.
Before setting your image to the GPUImage, create your image thumbnail. So it will load fast.
GPUImaage gpuImage=new GPUImage(context);
gpuImage.setImage(your image);
gpuImage.setFilter(choose your filter);
gpuImage.requestRender();
imageView.setImageBitmap(gpuImage.getBitmapWithFilterApplied());
gpuImage.deleteImage();
Related
I am trying to make an application with a ListView that include a Country Flag and name. This is so that the user can click on them and be shown images of the country that they wouldve taken before. However for about 3 seconds when the listview loads if i try to scroll it will sort of glitch and send me back to top. This is the code..
public class CountriesListAdapter extends ArrayAdapter {
private int resource;
private LayoutInflater inflater;
private List<CountryModel> countryModels;
private WeakReference<TextView> selectedCountryIdtxt;
private boolean useFilter;
private WeakReference<ProgressBar> progressBarWeakReference;
public int getSelectedCountryId() {
return selectedCountryId;
}
public void setSelectedCountryId(int selectedCountryId) {
this.selectedCountryId = selectedCountryId;
}
private int selectedCountryId;
public CountriesListAdapter(#NonNull WeakReference<Context> context, int resourceId, WeakReference<TextView> textView, #NonNull List<CountryModel> countryModelList, boolean useFilter, WeakReference<ProgressBar> progressBarWeakReference){
super(context.get(), resourceId, countryModelList);
selectedCountryIdtxt = textView;
resource = resourceId; //the id of the template file
inflater = LayoutInflater.from(context.get());
this.countryModels = countryModelList;
selectedCountryId = -1;
this.useFilter = useFilter;
this.progressBarWeakReference = progressBarWeakReference;
}
public int getCount() {
if (countryModels != null)
return countryModels.size();
return 0;
}
public CountryModel getItem(int position) {
if (countryModels != null)
return countryModels.get(position);
return null;
}
public long getItemId(int position) {
if (countryModels != null)
return countryModels.get(position).hashCode();
return 0;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// this method is automatically called for every object in our list
//basically it's called for every single row before it is generated
// this method is called per row
convertView = (ConstraintLayout) inflater.inflate(resource, null);
//the variable countryModel is fiiled with current object that is being processed
final CountryModel countryModel = countryModels.get(position);
TextView countryName = convertView.findViewById(R.id.countryNamelbl);
final ImageView countryFlag = convertView.findViewById(R.id.countryFlagimg);
final ImageView checked = convertView.findViewById(R.id.countryCheckedimg);
//this is done for every object in the list
assert countryModel != null;
countryName.setText(countryModel.getName());
Picasso.get().load(countryModel.getImage()).fit().into(countryFlag);
if(!useFilter) {
if (selectedCountryId == countryModel.getId()) {
checked.setVisibility(View.VISIBLE);
} else {
checked.setVisibility(View.INVISIBLE);
}
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!useFilter) {
if (checked.getVisibility() == View.VISIBLE) {
checked.setVisibility(View.INVISIBLE);
selectedCountryId = -1;
selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
} else {
if (selectedCountryId == -1) {
checked.setVisibility(View.VISIBLE);
selectedCountryId = countryModel.getId();
} else {
selectedCountryId = countryModel.getId();
notifyDataSetChanged();
}
selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
}
} else {
Intent i = new Intent(getContext(), PicturesActivity.class);
i.putExtra("countryId",countryModel.getId());
i.putExtra("countryName", countryModel.getName());
getContext().startActivity(i);
}
}
});
progressBarWeakReference.get().setVisibility(View.INVISIBLE);
return convertView;
}
public List<CountryModel> getCountryModels() {
return countryModels;
}
public void setCountryModels(List<CountryModel> countryModels) {
this.countryModels = countryModels;
}
}
The problem was actually in another class, i was calling the adapter for every list item instead of just once... oops.
Thanks for the replies though!
I have a custom adapter added to listview. Data is call logs from phone. I reduce list by show only records from 3 days. Problem is that when I try to scroll listview from top to bottom I have a huge lags. My Scroll isn't smooth. Is there any way to make listview scroll smoother?
Here is my custom adapter:
public class CallListAdapter extends ArrayAdapter<CallList> {
Activity activity;
public CallListAdapter(Context context, ArrayList<CallList> calls, Activity activity) {
super(context, 0, calls);
this.activity = activity;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final CallList callList = getItem(position);
int actualPosition = 0;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.call_list, parent, false);
}
final TextView call1 = convertView.findViewById(R.id.callNumber);
final TextView call2 = convertView.findViewById(R.id.callDate);
final TextView call3 = convertView.findViewById(R.id.conversationTime);
final TextView call4 = convertView.findViewById(R.id.callType);
final Button callView = convertView.findViewById(R.id.getViewName);
final ImageView bio = convertView.findViewById(R.id.lookBio);
final ImageView edit = convertView.findViewById(R.id.edit_call);
final ImageView block = convertView.findViewById(R.id.blockCall);
final ImageView call = convertView.findViewById(R.id.callUser);
final TextView bioLabel = convertView.findViewById(R.id.BioLabelSug);
final TextView editLabel = convertView.findViewById(R.id.NoteLabel);
final TextView blockLabel = convertView.findViewById(R.id.BlockLabelSug);
final TextView callLabel = convertView.findViewById(R.id.CallLabelSug);
final ConstraintLayout callContainer = convertView.findViewById(R.id.contact_container);
final ConstraintLayout bioContainer = convertView.findViewById(R.id.bio_container);
final ConstraintLayout blockContainer = convertView.findViewById(R.id.ignore_container);
final ConstraintLayout noteContainer = convertView.findViewById(R.id.note_container);
final TextView btnMarg = convertView.findViewById(R.id.buttonMargin);
final TextView callListNr2 = convertView.findViewById(R.id.callNumber2);
final LayoutInflater factory = activity.getLayoutInflater();
final View fullView = factory.inflate(R.layout.fragment_calls, null);
final RelativeLayout loading = fullView.findViewById(R.id.loadingBar);
String[] jsonData = new manageCalls().intentCallValues(position);
StringBuilder builder = new StringBuilder();
for (String s : jsonData) {
builder.append(s + "\n");
}
String str = builder.toString();
final String num = jsonData[0];
final String dat = jsonData[1];
final String typeCall = jsonData[2];
final String dur = jsonData[3];
final String authToken = SaveSharedPreferences.getPrefTokenName(getContext());
final Animation slideUp = AnimationUtils.loadAnimation(getContext(), R.anim.slideup);
final Animation slideDown = AnimationUtils.loadAnimation(getContext(), R.anim.slidedown);
final Handler handler = new Handler();
callView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (bioContainer.getVisibility() == View.GONE) {
callListNr2.setVisibility(View.GONE);
bio.setVisibility(View.VISIBLE);
bioLabel.setVisibility(View.VISIBLE);
edit.setVisibility(View.VISIBLE);
editLabel.setVisibility(View.VISIBLE);
} else if (bioContainer.getVisibility() == View.VISIBLE) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
bio.setVisibility(View.GONE);
callContainer.setVisibility(View.GONE);
bioContainer.setVisibility(View.GONE);
noteContainer.setVisibility(View.GONE);
blockContainer.setVisibility(View.GONE);
}
}, 300);
}
}
});
if (actualPosition != position) {
if (bioContainer.getVisibility() == View.VISIBLE) {
bioContainer.setVisibility(View.GONE);
callContainer.setVisibility(View.GONE);
noteContainer.setVisibility(View.GONE);
blockContainer.setVisibility(View.GONE);
}
actualPosition = position;
}
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
call.setEnabled(false);
loading.setVisibility(View.VISIBLE);
Intent intentCall = new Intent(view.getContext(), CallUserActivity.class);
intentCall.putExtra("number", num);
intentCall.putExtra("authToken", authToken);
intentCall.putExtra("Date", dat);
activity.startActivityForResult(intentCall, position);
handler.postDelayed(new Runnable() {
#Override
public void run() {
call.setEnabled(true);
loading.setVisibility(View.GONE);
}
}, 1000);
}
});
call2.setText(callList.callDate);
call3.setText(callList.conversationTime);
call4.setText(callList.callType);
return convertView;
}
}
Try use ViewHolder and use AsyncTask to load bitmap.
You can try this way.
private static class ViewHolder {
public TextView call1;
public TextView call2;
public TextView call3;
public TextView call4;
public Button callView;
public ImageView bio;
public ImageView edit;
public ImageView block;
public ImageView call;
public TextView bioLabel;
public TextView editLabel;
public TextView blockLabel;
public TextView callLabel;
public ConstraintLayout callContainer;
public ConstraintLayout bioContainer;
public ConstraintLayout blockContainer;
public ConstraintLayout noteContainer;
public TextView btnMarg;
public TextView callListNr2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// inflate the layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.call1 = convertView.findViewById(R.id....);
holder.call2 = convertView.findViewById(R.id....);
//Same for all other views
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.call1.setText(....);
//Lazy load for bitmap
loadBitmap(yourFileName..., bio)
return convertView;
}
static class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
#Override
protected Bitmap doInBackground(String... params) {
return decodeSampledBitmapFromResource(params[0], 300, 300);
}
// Once complete, see if ImageView is still around and set bitmap.
#Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
public void loadBitmap(String fileName, ImageView imageView) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(fileName);
}
public static Bitmap decodeSampledBitmapFromResource(String fileName,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(fileName, options);
}
Your getview is HUGE.
Your (if convertview==null) has basically almost no effect as you're setting up the view again anyways.
What you need to do is refactor the getview to not be so slow. one thing you can do is create a class that has all the findviews done already for you and put that then in the .tag of the converted view. change your onclicks to use that as well, in a manner where you don't have to recreate them(other ways to do that exist as well).
ideally your code for if you have a converted view already should be just the .settexts().
depending on the size of your list, you could just get away with creating a view for each callist and avoid recycling the converted views alltogether, in such case you could just create them in advance.
also depending on the size of your list you could just get away with creating a just a simple linearlayout instead inside a scrollview. if your list isn't huge and it's not for some old phones, it works just fine as well (Don't knock on this as bad advice until you try on your phone how huge it can be before a listview starts making more sense).
I have an ArrayList which is bound to the listview, the custom row has a textview and an imageview, now when I click on any row I have given two functionality
1. Toast message to display position: which is displaying properly.
2. Open camera to capture an image and set that particular image to the row which was clicked.
Now the problem which I am facing is :
The image gets set but always to the last row and not to the row where it was clicked and when I click on the last row to capture image while setting the image it says IndexOutofBoundException
The code I have tried :
public class DocumentsKYCAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<DocumentItem> gridName;
ArrayList<Integer> selectedDocumentId = new ArrayList<Integer>();
ArrayList<String> selectedDocumentNames = new ArrayList<String>();
ArrayList<Bitmap> selectedImages = new ArrayList<Bitmap>();
private Bitmap[] gridImage;
Documents_KYC activity;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String IMAGE_DIRECTORY_NAME = "Imageview";
private Uri fileUri;
ImageView imageView;
public static byte[] b;
public static String encodedImageStr1;
int imageCapturedPosition;
public DocumentsKYCAdapter(Documents_KYC activity,
ArrayList<DocumentItem> gridName,ArrayList<Integer>
selectedDocumentId,ArrayList<String> selectedDocumentNames) {
this.activity = activity;
this.gridImage = gridImage;
this.gridName = gridName;
this.selectedDocumentNames = selectedDocumentNames;
this.selectedDocumentId = selectedDocumentId;
}
#Override
public int getCount() {
return selectedDocumentNames.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent) {
View grid;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.documents_kyc_row, null);
} else {
grid = (View) convertView;
}
final String documentItemName =
selectedDocumentNames.get(position);
final int documentItemId = selectedDocumentId.get(position);
TextView textView = (TextView) grid.findViewById(R.id.gridName);
imageView = (ImageView) grid.findViewById(R.id.gridImage);
imageView.setTag(position);
textView.setText(documentItemName);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageCapturedPosition = position;
Toast.makeText(activity,"Id"+documentItemId+"
,Position"+imageCapturedPosition,Toast.LENGTH_LONG).
show();
imageView.getTag(position);
captureImage();
}
});
return grid;
}
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
activity.startActivityForResult(intent, MEDIA_TYPE_IMAGE);
}
public void onActivityResult(int requestCode, int resultCode, Intent
data) {
try {
if (requestCode == MEDIA_TYPE_IMAGE && resultCode ==
activity.RESULT_OK) {
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bitmap = Utility.decodeFile(fileUri.getPath(),
options);
FileOutputStream out = null;
try {
out = new FileOutputStream(fileUri.getPath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
ByteArrayOutputStream baos = new
ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80,
baos);
b = baos.toByteArray();
encodedImageStr1 = Base64.encodeToString(b,
Base64.DEFAULT);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
selectedImages.add(bitmap);
imageView.setImageBitmap(selectedImages.
get(imageCapturedPosition));
} else if (resultCode == activity.RESULT_CANCELED) {
Toast.makeText(activity,
"User cancelled image capture",
Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(activity,
"Sorry! Failed to capture image",
Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Custom Class :
public class BitmapModel {
Bitmap imageId;
ArrayList<String> documentNamesList;
public ArrayList<String> getDocumentNamesList() {
return documentNamesList;
}
public void setDocumentNamesList(ArrayList<String> documentNamesList) {
this.documentNamesList = documentNamesList;
}
public Bitmap getImageId() {
return imageId;
}
public void setImageId(Bitmap imageId) {
this.imageId = imageId;
}
}
Your issue is in the line -
selectedImages.add(bitmap);
Here whenever you add an image to your arrayList it always adds at the last position and then when you do imageView.setImageBitmap(selectedImages.get(imageCapturedPosition)); It tries to get the value for the position you selected which is not necessarily the last position.
The better way for you to do is create a Custom Class with Bitmap and selectedDocumentNames as part of the class then each object of the class would represent a name and an image associated with it.
Now when you capture the image, assign the image to the class Bitmap and then populate your listview with that Bitmap.
The workaround for your present code code be to either add the image into a particular position in array denoted by imageCapturedPosition or create a hashmap of type position,bitmap and then store it with the selected position. though i would not recommend any of these workarounds as they would cause other problems in future like memory leaks and positional movements in arrays etc and you would have to take care of these things
In one of activity contains RecyclerView in which certain items are rendered and on clicking any item its moving to another activity which is having a massive data from a server, its like a detail page of news along with images from server to render in its gallery section, using ViewPager along PagerAdapter(Some news articles may contains 20-30 images.
Whenever I move forward and backward from these activites my application crashes with OutOfMemory Error.
So far I have tried calling System.gc() and then finish() after moving back from detail activity to the one containing RecyclerView but still getting OutOfMemory errors.
I know its a memory leak as its getting large amount of data and then rendering at different places in activity but don't know how to handle it.
I have read some solutions mentioning android:largeHeap but it also doesn't guarentee that you may get large heap with some other cons like frequent pausings.
PagerAdapter:
public class SingleNewsGalleryAdapter extends PagerAdapter {
private String URL="some url";
Context mContext;
LayoutInflater mLayoutInflater;
List<String> newsList= Collections.emptyList();
int width=-1;
int heigth=-1;
public SingleNewsGalleryAdapter(Context context,List<String> newsList) {
this.newsList = newsList;
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return newsList.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
final int pos = position;
View itemView = mLayoutInflater.inflate(R.layout.single_news_pager_layout, container, false);
final Holder holder = new Holder();
holder.imageView = (ImageView) itemView.findViewById(R.id.slidr_img);
holder.left = (ImageView) itemView.findViewById(R.id.swipe_left);
if(position==0){
holder.left.setColorFilter(ContextCompat.getColor(mContext, R.color.trans_black));
}
holder.right= (ImageView) itemView.findViewById(R.id.swipe_right);
if(position==(newsList.size()-1)){
holder.right.setColorFilter(ContextCompat.getColor(mContext, R.color.trans_black));
}
holder.left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(pos>0){
int desc = pos-1;
if(mContext instanceof SingleNewsActivity){
((SingleNewsActivity)mContext).changePage(desc);
}
}
}
});
holder.right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(pos<newsList.size()){
if(mContext instanceof SingleNewsActivity){
int inc = pos+1;
((SingleNewsActivity)mContext).changePage(inc);
//Toast.makeText(mContext,pos+"",Toast.LENGTH_LONG).show();
}
}
}
});
String news = newsList.get(position);
//imageView.setImageResource(newsList.get(position).getNews_photo());
int dpiCheck=0;
String res="";
switch (mContext.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
dpiCheck=1;
break;
case DisplayMetrics.DENSITY_MEDIUM:
dpiCheck=2;
break;
case DisplayMetrics.DENSITY_HIGH:
dpiCheck=3;
break;
case DisplayMetrics.DENSITY_XHIGH:
dpiCheck=4;
break;
}
if(dpiCheck==3){
res="525x255x1-";
}
else if(dpiCheck==4){
res="700x340x1-";
}
// res = width+"x"+height+"x1-";
String url = URL+res+news;
Uri uri = Uri.parse(url);
Picasso.with(this.mContext).load(uri).into(holder.imageView);
container.addView(itemView);
return itemView;
}
#Override
public float getPageWidth(int position) {
return 1f;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
static class Holder {
ImageView imageView;
ImageView left;
ImageView right;
}
}
and my RecyclerAdapter from the listing activity:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {
String URL = "some url";
List<News> newsList = Collections.emptyList();
Context ctx;
int layoutID;
public NewsAdapter(List<News> newsList, Context ctx, int layout) {
this.layoutID = layout;
this.newsList = newsList;
this.ctx = ctx;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(layoutID, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
News news = newsList.get(position);
String title = news.getNews_title();
holder.title.setText(title);
String desc = news.getNews_description();
String htmlDesc;
String formatedDesc =desc.substring(0, Math.min(desc.length(), 50))+"...";
if (Build.VERSION.SDK_INT >= 24) {
holder.desc.setText(Html.fromHtml(desc, Html.FROM_HTML_SEPARATOR_LINE_BREAK_DIV));
} else {
holder.desc.setText(Html.fromHtml(desc));
}
String authorName;
if(news.getAuthor().trim().equals("") || news.getAuthor()==null){
authorName="Anonymous";
}
else{
authorName = news.getAuthor().trim();
}
holder.author.setText(authorName);
//Format Date
SimpleDateFormat fromUser = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("d/M/yy");
String formatedDate="";
try {
formatedDate = myFormat.format(fromUser.parse(news.getNews_add_date()));
} catch (ParseException e) {
e.printStackTrace();
}
holder.date.setText(formatedDate);
//view counter format
Integer viewCount = Integer.parseInt(news.getViews());
DecimalFormat formatter = new DecimalFormat("##,##,###");
String number = formatter.format(viewCount);
holder.views.setText(number);
//comments counter format
Integer commentCount = Integer.parseInt(news.getComments());
formatter = new DecimalFormat("##,##,###");
String c_number = formatter.format(commentCount);
holder.comments.setText(c_number);
int dpiCheck=0;
String res="";
switch (ctx.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
dpiCheck=1;
break;
case DisplayMetrics.DENSITY_MEDIUM:
dpiCheck=2;
break;
case DisplayMetrics.DENSITY_HIGH:
dpiCheck=3;
break;
case DisplayMetrics.DENSITY_XHIGH:
dpiCheck=4;
break;
}
if(dpiCheck==3){
res="165x165x1-";
}
else if(dpiCheck==4){
res="220x220x1-";
}
String url = URL+res+news.getNews_photo();
Uri uri = Uri.parse(url);
Picasso.with(this.ctx).load(uri).into(holder.image);
}
#Override
public int getItemCount() {
return newsList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title,desc,date,views,comments,author;
ImageView image;
public MyViewHolder(View view) {
super(view);
title = (TextView)view.findViewById(R.id.news_title);
author = (TextView)view.findViewById(R.id.author);
desc =(TextView)view.findViewById(R.id.news_desc);
date=(TextView)view.findViewById(R.id.date);
views = (TextView)view.findViewById(R.id.view_counter);
comments = (TextView)view.findViewById(R.id.comment_counter);
image = (ImageView) view.findViewById(R.id.news_img);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String id="";
String cat="";
String brand="";
if(Build.VERSION.SDK_INT>=24){
id= newsList.get(getAdapterPosition()).getNews_id();
cat = newsList.get(getAdapterPosition()).getCat_id();
brand = newsList.get(getAdapterPosition()).getBrand_id();
}
else{
id= newsList.get(getPosition()).getNews_id();
cat = newsList.get(getPosition()).getCat_id();
brand = newsList.get(getPosition()).getBrand_id();
}
Intent intent = new Intent(view.getContext(), SingleNewsActivity.class);
intent.putExtra("NEWSID",id);
intent.putExtra("NEWSCAT",cat);
intent.putExtra("NEWSBRAND",brand);
ctx.startActivity(intent);
// Toast.makeText(view.getContext(), "position = " + id, Toast.LENGTH_SHORT).show();
}
});
}
}
public void setFilter(List<News> newList){
this.newsList = new ArrayList<>();
this.newsList.addAll(newList);
notifyDataSetChanged();
}
public void loadMore(List<News> newList){
this.newsList.addAll(newList);
notifyDataSetChanged();
}
}
The Error Statement OutOfMemory Error at NewsAdapter.onCreateViewHolder(NewsAdapter.java:49)
where the onCreateViewHolder is called.
I am looking to build something similar to the settings UI of system android. I want something like a few checkboxpreferences, switchpreferences, edittextpreferences on the launch of application and then when user selects one preference open a fragment but i am just not able to figure that out.
I have referred Settings guide but it insists on using preference header. While displaying headers there is an unlikely overhead i am facing of displaying texts which in turn will load fragments.
For example,
My preference header is something like :
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="${packageName}.${activityClass}$GeneralPreferenceFragment"
android:title="#string/pref_header_general" />
<header
android:fragment="${packageName}.${activityClass}$NotificationPreferenceFragment"
android:title="#string/pref_header_notifications" />
<header
android:fragment="${packageName}.${activityClass}$DataSyncPreferenceFragment"
android:title="#string/pref_header_data_sync" />
</preference-headers>
and just to load the actual data, i am having to use it. The actual data will have checkboxes and edittexts.
It would be great if someone gave some insights on this. It would be of great help if i could launch the actual fragment data on loading of screen. Better if i could have control of what fragment to call and call other fragments when a fragment item is selected.
To create custom preference headers, with switches and such, you need to extend PreferenceActivity with Headers as the Android docs describe and then override PreferenceActivity.setListAdapter to create your own list adapter, which creates the custom views. I made a pastebin with the code from the actual android settings activity to help you out. http://pastebin.com/RhSndGCQ
#Override
public void onBuildHeaders(List<Header> headers) {
loadHeadersFromResource(R.xml.settings_headers, headers);
updateHeaderList(headers);
}
#Override
public void setListAdapter(ListAdapter adapter) {
if (adapter == null) {
super.setListAdapter(null);
} else {
super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
}
}
private static class HeaderAdapter extends ArrayAdapter<Header> {
static final int HEADER_TYPE_CATEGORY = 0;
static final int HEADER_TYPE_NORMAL = 1;
static final int HEADER_TYPE_SWITCH = 2;
private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
private final WifiEnabler mWifiEnabler;
private final BluetoothEnabler mBluetoothEnabler;
private final ProfileEnabler mProfileEnabler;
private AuthenticatorHelper mAuthHelper;
private static class HeaderViewHolder {
ImageView icon;
TextView title;
TextView summary;
Switch switch_;
}
private LayoutInflater mInflater;
static int getHeaderType(Header header) {
if (header.fragment == null && header.intent == null) {
return HEADER_TYPE_CATEGORY;
} else if (header.id == R.id.wifi_settings
|| header.id == R.id.bluetooth_settings
|| header.id == R.id.profiles_settings) {
return HEADER_TYPE_SWITCH;
} else {
return HEADER_TYPE_NORMAL;
}
}
#Override
public int getItemViewType(int position) {
Header header = getItem(position);
return getHeaderType(header);
}
#Override
public boolean areAllItemsEnabled() {
return false; // because of categories
}
#Override
public boolean isEnabled(int position) {
return getItemViewType(position) != HEADER_TYPE_CATEGORY;
}
#Override
public int getViewTypeCount() {
return HEADER_TYPE_COUNT;
}
#Override
public boolean hasStableIds() {
return true;
}
public HeaderAdapter(Context context, List<Header> objects,
AuthenticatorHelper authenticatorHelper) {
super(context, 0, objects);
mAuthHelper = authenticatorHelper;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Temp Switches provided as placeholder until the adapter replaces these with actual
// Switches inflated from their layouts. Must be done before adapter is set in super
mWifiEnabler = new WifiEnabler(context, new Switch(context));
mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
mProfileEnabler = new ProfileEnabler(context, null, new Switch(context));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
Header header = getItem(position);
int headerType = getHeaderType(header);
View view = null;
if (convertView == null || headerType == HEADER_TYPE_SWITCH) {
holder = new HeaderViewHolder();
switch (headerType) {
case HEADER_TYPE_CATEGORY:
view = new TextView(getContext(), null,
android.R.attr.listSeparatorTextViewStyle);
holder.title = (TextView) view;
break;
case HEADER_TYPE_SWITCH:
view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
false);
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.title = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.summary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
break;
case HEADER_TYPE_NORMAL:
view = mInflater.inflate(
R.layout.preference_header_item, parent,
false);
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.title = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.summary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
break;
}
view.setTag(holder);
} else {
view = convertView;
holder = (HeaderViewHolder) view.getTag();
}
// All view fields must be updated every time, because the view may be recycled
switch (headerType) {
case HEADER_TYPE_CATEGORY:
holder.title.setText(header.getTitle(getContext().getResources()));
break;
case HEADER_TYPE_SWITCH:
// Would need a different treatment if the main menu had more switches
if (header.id == R.id.wifi_settings) {
mWifiEnabler.setSwitch(holder.switch_);
} else if (header.id == R.id.bluetooth_settings) {
mBluetoothEnabler.setSwitch(holder.switch_);
} else if (header.id == R.id.profiles_settings) {
mProfileEnabler.setSwitch(holder.switch_);
}
// No break, fall through on purpose to update common fields
//$FALL-THROUGH$
case HEADER_TYPE_NORMAL:
if (header.extras != null
&& header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
String accType = header.extras.getString(
ManageAccountsSettings.KEY_ACCOUNT_TYPE);
ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
lp.width = getContext().getResources().getDimensionPixelSize(
R.dimen.header_icon_width);
lp.height = lp.width;
holder.icon.setLayoutParams(lp);
Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
holder.icon.setImageDrawable(icon);
} else {
holder.icon.setImageResource(header.iconRes);
}
holder.title.setText(header.getTitle(getContext().getResources()));
CharSequence summary = header.getSummary(getContext().getResources());
if (!TextUtils.isEmpty(summary)) {
holder.summary.setVisibility(View.VISIBLE);
holder.summary.setText(summary);
} else {
holder.summary.setVisibility(View.GONE);
}
break;
}
return view;
}
public void resume() {
mWifiEnabler.resume();
mBluetoothEnabler.resume();
mProfileEnabler.resume();
}
public void pause() {
mWifiEnabler.pause();
mBluetoothEnabler.pause();
mProfileEnabler.pause();
}
}