1 |
package dk.thoerup.side9; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.io.RandomAccessFile; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import android.app.Activity; |
9 |
import android.content.Intent; |
10 |
import android.graphics.Bitmap; |
11 |
import android.graphics.BitmapFactory; |
12 |
import android.net.Uri; |
13 |
import android.os.Bundle; |
14 |
import android.util.Log; |
15 |
import android.view.Menu; |
16 |
import android.view.MenuItem; |
17 |
import android.view.MotionEvent; |
18 |
import android.view.View; |
19 |
import android.widget.ImageView; |
20 |
import android.widget.TextView; |
21 |
|
22 |
public class PictureView extends Activity { |
23 |
final static int CONTEXT_VIEWIMG = 1000; |
24 |
|
25 |
final static String TAG = "Side9Pigen"; |
26 |
|
27 |
TextView mDescription; |
28 |
TextView mCaption; |
29 |
ImageView mImageView; |
30 |
ArrayList<String> mImagePaths; |
31 |
int mIndex; |
32 |
|
33 |
|
34 |
Bitmap mBitmap; |
35 |
|
36 |
@SuppressWarnings("unchecked") |
37 |
@Override |
38 |
public void onCreate(Bundle savedInstanceState) { |
39 |
super.onCreate(savedInstanceState); |
40 |
setContentView(R.layout.pictureview); |
41 |
|
42 |
mImagePaths = (ArrayList<String>) getIntent().getSerializableExtra("files"); |
43 |
mIndex = getIntent().getIntExtra("index", 0); |
44 |
|
45 |
|
46 |
mImageView = (ImageView) findViewById(R.id.imageview); |
47 |
mImageView.setOnTouchListener( new Touch() ); |
48 |
|
49 |
mCaption = (TextView) findViewById(R.id.caption); |
50 |
mDescription = (TextView) findViewById(R.id.description); |
51 |
|
52 |
|
53 |
loadImage(); |
54 |
|
55 |
} |
56 |
|
57 |
|
58 |
|
59 |
private void loadImage(int newIndex) { |
60 |
Log.e(TAG, "NewIndex " + newIndex); |
61 |
if (newIndex != mIndex) { |
62 |
mIndex = newIndex; |
63 |
loadImage(); |
64 |
} |
65 |
} |
66 |
|
67 |
private void loadImage() { |
68 |
String currentFile = mImagePaths.get(mIndex); |
69 |
mBitmap = BitmapFactory.decodeFile( currentFile ); |
70 |
mImageView.setImageBitmap(mBitmap); |
71 |
|
72 |
String pathParts[] = currentFile.split("/"); |
73 |
String fileName = pathParts[ pathParts.length -1]; |
74 |
String desc = "" + (mIndex +1) + "/" + mImagePaths.size() + " - " + fileName; |
75 |
mDescription.setText(desc); |
76 |
|
77 |
String infoFileName = currentFile.replace(".jpg", ".txt"); |
78 |
|
79 |
File infoFile = new File(infoFileName); |
80 |
if (infoFile.exists() ) { |
81 |
String caption = getFileContent(infoFile); |
82 |
mCaption.setVisibility(View.VISIBLE); |
83 |
mCaption.setText(caption); |
84 |
|
85 |
} else { |
86 |
mCaption.setVisibility(View.INVISIBLE); |
87 |
} |
88 |
} |
89 |
|
90 |
String getFileContent(File filename) { |
91 |
try { |
92 |
RandomAccessFile raf = new RandomAccessFile(filename, "r"); |
93 |
int size = (int)raf.length(); |
94 |
byte buf[] = new byte[size]; |
95 |
|
96 |
raf.readFully(buf); |
97 |
return new String(buf); |
98 |
} catch (IOException e) { |
99 |
Log.e(TAG, "Error", e); |
100 |
return ""; |
101 |
} |
102 |
} |
103 |
|
104 |
|
105 |
|
106 |
@Override |
107 |
public boolean onCreateOptionsMenu(Menu menu) { |
108 |
MenuItem item = menu.add(Menu.NONE, CONTEXT_VIEWIMG, Menu.NONE, "Built-in viewer"); |
109 |
item.setIcon( android.R.drawable.ic_menu_gallery); |
110 |
return true; |
111 |
} |
112 |
|
113 |
|
114 |
|
115 |
@Override |
116 |
public boolean onOptionsItemSelected(MenuItem item) { |
117 |
boolean res; |
118 |
|
119 |
switch(item.getItemId()) { |
120 |
case CONTEXT_VIEWIMG: |
121 |
String uri = "file://" + mImagePaths.get(mIndex); |
122 |
|
123 |
Intent i = new Intent(Intent.ACTION_VIEW); |
124 |
i.setDataAndType(Uri.parse(uri), "image/jpeg"); |
125 |
startActivity(i); |
126 |
res = true; |
127 |
break; |
128 |
default: |
129 |
res = super.onOptionsItemSelected(item); |
130 |
} |
131 |
|
132 |
return res; |
133 |
} |
134 |
|
135 |
class Touch implements View.OnTouchListener { |
136 |
|
137 |
Float firstX = null; |
138 |
|
139 |
|
140 |
@Override |
141 |
public boolean onTouch(View v, MotionEvent event) { |
142 |
if (event.getAction() == MotionEvent.ACTION_DOWN) { |
143 |
firstX = event.getX(); |
144 |
} else { |
145 |
if (firstX != null) { |
146 |
float x = event.getX(); |
147 |
float diff = firstX - x; |
148 |
|
149 |
if (diff > 150) { |
150 |
int newIndex = Math.min(mIndex+1, mImagePaths.size() -1); |
151 |
loadImage(newIndex); |
152 |
firstX = null; |
153 |
return false; |
154 |
} |
155 |
|
156 |
if (diff < -150) { |
157 |
int newIndex = Math.max(mIndex-1, 0); |
158 |
loadImage(newIndex); |
159 |
firstX = null; |
160 |
return false; |
161 |
} |
162 |
} |
163 |
} |
164 |
|
165 |
return true; |
166 |
} |
167 |
|
168 |
} |
169 |
} |