I'm new to Android and I'm using API version 4.4. I'm using a ViewPager to display a fragment exactly as described in this link. http://developer.android.com/training/animation/screen-slide.html.
What my app does is it let the user to take a picture from camera and when he clicks the next button he can add another image and he can go on keep doing that(also he can come to the previous page). I'm displaying his picture inside a imageView.
I update the imageView in the fragment in the onActivityResult
method inside Fragment. The issue is, when I change imageView (To his camera picture) it happesn to the next page in the viewPager not in the current page. Also what I noticed is onResume
function in my Fragment class fires multiple times when the page loads for the first time.
This is my code so far,
MainActivity.java
private static final int MAX_IMAGES = 10;
protected int currentIndex=0; // Which page we are currently in
private ImageFragment fragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewPager = (ViewPager)findViewById(R.id.viewPager);
pageAdapter = new TicketImageAdapter(getSupportFragmentManager());
viewPager.setAdapter(pageAdapter);
//LinearLayout uploadImage = ((LinearLayout)findViewById(R.id.uploadImage));
Button nextButton = (Button)findViewById(R.id.nextBttn);
Button saveButton = (Button)findViewById(R.id.saveButton);
Button previousButton = (Button)findViewById(R.id.previousBttn);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fragment != null)
{
fragment.addTicketImage();
displayTicketImages();
}
if(MAX_IMAGES > currentIndex)
{
currentIndex++;
Log.d("Currect Index; ",currentIndex+"");
setCurrentItem (currentIndex, true);
}
}
});
// Set current page fragment so I can access it's methods
public void setFragment(TicketImageFragment fragment)
{
this.fragment = fragment;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//Don't worry about fragment been null, coz it is not. I Set the fragment in the on
if(this.fragment != null)
{
this.fragment.onActivityResult(requestCode, resultCode, data);
}
}
public static class TicketImageAdapter extends FragmentPagerAdapter {
public TicketImageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return MAX_IMAGES;
}
@Override
public Fragment getItem(int position) {
return new ImageFragment();
}
}
}
ImageFragment.java
This is where I redirect the common user to the camera and display his picture like a preview in a imageView.
private TextView firstText;
private MainActivity activity;
View view;
ImageView imgPreview;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.activity_fragment,container,false);
LinearLayout uploadImageBttn = ((LinearLayout)view.findViewById(R.id.uploadImageButton));
firstText = ((TextView)view.findViewById(R.id.note));
imgPreview = (ImageView)view.findViewById(R.id.ticketImage);
uploadImageBttn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.helper = new CameraHelper(v.getContext());
activity.helper.captureImage(); // Capture image and save it
}
});
this.activity = (TicketImageActivity)getActivity();
// Set fragment So the activity can call onActivityResult on this fragment
this.activity.setFragment(this);
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(imgPreview == null)
Log.d("imagePreviewView", "NULL");
else
Log.d("UploadButton", "NOT NULL");
// if the result is capturing Image
if (requestCode == CameraHelper.getCode()) {
if (resultCode == Activity.RESULT_OK) {
//ImageView imgPreview = (ImageView)view.findViewById(R.id.ticketImage);
previewCapturedImage(imgPreview,"this is the image path");// Preview captured image
} else if (resultCode == Activity. RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(view.getContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(view.getContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
}
public void previewCapturedImage(ImageView imgPreview,String imagePath) {
try {
imgPreview.setVisibility(View.VISIBLE); // If the image view is hidden, show it
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
try {
Bitmap bitmap = BitmapFactory.decodeFile(mImagePath,
options);
ExifInterface exif = new ExifInterface(mImagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
}
else if (orientation == 3) {
matrix.postRotate(180);
}
else if (orientation == 8) {
matrix.postRotate(270);
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap
imgPreview.setImageBitmap(bitmap);
}
catch (Exception e) {
Log.d("Exception :",e.getMessage());
}
//date = null;
} catch (NullPointerException e) {
e.printStackTrace();
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="400dp"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previousBttn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:text="@string/Previous" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="@string/saveButton" />
<Button
android:id="@+id/nextBttn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:text="@string/Next" />
</LinearLayout>
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="320dp"
android:orientation="vertical"
android:id="@+id/uploadImageButton"
android:background="@drawable/customborder">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/uploadImageText"/>
<ImageView
android:id="@+id/ticketImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"/>
</LinearLayout>
<EditText
android:id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/enterNote" />
Image capturing and saving it in the album works perfectly fine. The only issue is the image is displayed in the wrong page. When I Capture the image in the first page it displays the preview in the second page and when I capture it in the another page it shows in the next page.
Any help would be much appreciated. Thanks!