I have an image carousel I've built for my app using a ViewFlipper but the quality of the images stored on the device are very poor. I've using the following:
String fileLoc = mediaData.get("fileLoc");
ImageView imageItem = new ImageView(getApplicationContext());
InputStream imageStream = null;
try {
File file = new File(fileLoc);
imageStream = new FileInputStream(file);
imageItem.setImageBitmap(Configurator.decodeSampledBitmapFromResource(getResources(), fileLoc, 100, 100));
} catch...
Memory is a concern as there could be any number of images displayed.
I did find this:
Resource to bitmap conversion results in poor quality
But I can't see how to translate it to my situation.
Per @Ultimo_m , Here is detail on what needs to happen:
- The user is presented with a screen that has three sections based on previous options selected: pdf documents, images, and videos.
- The user selects an image from the set shown
- User is taken to a screen that shows the selected item
- Swiping from right to left shows the next image in that set
- Swiping from left to right shows the previous image in the set
Right now I'm using a ViewFlipper:
- I pass an array of each image's location via a SharedPreference
- I also pass the key location of the current image
- I loop through the array to build the ViewFlipper
- I use the current key to set the current view in the flipper
Using the selected library from @Ultimo_m how do I tell the library the current image to show as well what the next and previous image will be?
----- EDIT -----
I have Univeral Image loader working on the current image when tapped from the previous screen. I can't find in the docs how to handle the swipe event and tell the library what to display next. There is a screenshot on the library's github of a swipe event in progress but how do I leverage this?
----- EDIT -----
App is crashing. Throwing a NullPointer at imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));. I've made the xml files as you specified. I did make adjustments to your code to get my array from the SharedPref I've catlogged my array that I'm passing (imageArraySet) and the array is correct. I've pasted the full error log after the code:
public class Viewer extends baseActivity {
clientDB clientDB = new ClientDB(this);
public static final String PREFS_NAME = "myPrefs";
SharedPreferences storedInfo;
String chosenImg;
ViewPager imageLoadView;
DisplayImageOptions options;
String[] imageArraySet;
//LoadImageUtil mLoadImageUtil;
private static final String STATE_POSITION = "STATE_POSITION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewer);
Bundle bundle = getIntent().getExtras();
assert bundle != null;
storedInfo = getSharedPreferences(PREFS_NAME,0);
String mediaID = storedInfo.getString("imgId", null); //ids of all passed images
String chosenImg = storedInfo.getString("chosenImg", null); //id of current image
String[] imageArray = mediaID.split(",");
imageArraySet = clientDB.getMediaDataSet(imageArray);
int pagerPosition = 0;
imageLoadView = (ViewPager) findViewById(R.id.imageLoadView);
imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));
imageLoadView.setCurrentItem(pagerPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, imageLoadView.getCurrentItem());
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
assert imageLayout != null;
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
//Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
}
Error log:
6-12 16:41:30.455: W/dalvikvm(19960): threadid=1: thread exiting with uncaught exception (group=0x41fdfe10)
06-12 16:41:30.455: E/AndroidRuntime(19960): FATAL EXCEPTION: main
06-12 16:41:30.455: E/AndroidRuntime(19960): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.client.clientconfigurator/com.client.clientconfigurator.Viewer}: java.lang.NullPointerException
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.access$700(ActivityThread.java:150)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.os.Looper.loop(Looper.java:176)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.main(ActivityThread.java:5279)
06-12 16:41:30.455: E/AndroidRuntime(19960): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 16:41:30.455: E/AndroidRuntime(19960): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 16:41:30.455: E/AndroidRuntime(19960): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-12 16:41:30.455: E/AndroidRuntime(19960): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-12 16:41:30.455: E/AndroidRuntime(19960): at dalvik.system.NativeStart.main(Native Method)
06-12 16:41:30.455: E/AndroidRuntime(19960): Caused by: java.lang.NullPointerException
06-12 16:41:30.455: E/AndroidRuntime(19960): at com.client.clientconfigurator.Viewer.onCreate(Viewer.java:92)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.Activity.performCreate(Activity.java:5267)