package dk.thoerup.side9; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.util.ArrayList; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class PictureView extends Activity { final static String TAG = "Side9Pigen"; TextView mDescription; TextView mCaption; ImageView mImageView; ArrayList mImagePaths; int mIndex; Bitmap mBitmap; @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pictureview); mImagePaths = (ArrayList) getIntent().getSerializableExtra("files"); mIndex = getIntent().getIntExtra("index", 0); mImageView = (ImageView) findViewById(R.id.imageview); mImageView.setOnTouchListener( new Touch() ); mDescription = (TextView) findViewById(R.id.description); mCaption = (TextView) findViewById(R.id.caption); loadImage(); } private void loadImage(int newIndex) { Log.e(TAG, "NewIndex " + newIndex); if (newIndex != mIndex) { mIndex = newIndex; loadImage(); } } private void loadImage() { String currentFile = mImagePaths.get(mIndex); mBitmap = BitmapFactory.decodeFile( currentFile ); mImageView.setImageBitmap(mBitmap); String pathParts[] = currentFile.split("/"); String fileName = pathParts[ pathParts.length -1]; String desc = "" + (mIndex +1) + "/" + mImagePaths.size() + " - " + fileName; mDescription.setText(desc); String infoFileName = currentFile.replace(".jpg", ".txt"); File infoFile = new File(infoFileName); if (infoFile.exists() ) { String caption = getFileContent(infoFile); mCaption.setVisibility(View.VISIBLE); mCaption.setText(caption); } else { mCaption.setVisibility(View.INVISIBLE); } } String getFileContent(File filename) { try { RandomAccessFile raf = new RandomAccessFile(filename, "r"); int size = (int)raf.length(); byte buf[] = new byte[size]; raf.readFully(buf); return new String(buf); } catch (IOException e) { Log.e(TAG, "Error", e); return ""; } } class Touch implements View.OnTouchListener { Float firstX = null; @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { firstX = event.getX(); } else { if (firstX != null) { float x = event.getX(); float diff = firstX - x; if (diff > 150) { int newIndex = Math.min(mIndex+1, mImagePaths.size() -1); loadImage(newIndex); firstX = null; return false; } if (diff < -150) { int newIndex = Math.max(mIndex-1, 0); loadImage(newIndex); firstX = null; return false; } } } return true; } } }