1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
package org.bienvenidoainternet.baiparser;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import org.bienvenidoainternet.baiparser.structure.BoardItem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
public class ViewerActivity extends AppCompatActivity {
private SubsamplingScaleImageView imageView;
private GifImageView gifView;
private BoardItem bi;
File imagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
// int themeId = Integer.valueOf(settings.getString("setting_theme", "1")), currentThemeId = R.style.AppTheme;
// switch (themeId) {
// case 1:
// currentThemeId = R.style.AppTheme_NoActionBar;
// break;
// case 2:
// currentThemeId = R.style.AppTheme_Dark;
// break;
// case 3:
// currentThemeId = R.style.AppTheme_HeadLine;
// break;
// case 4:
// currentThemeId = R.style.AppTheme_Black;
// break;
// }
// setTheme(currentThemeId);
if (savedInstanceState != null){
bi = savedInstanceState.getParcelable("boardItem");
}
if (getIntent().getExtras() != null){
bi = getIntent().getParcelableExtra("boardItem");
}
setContentView(R.layout.activity_viewer);
imageView = (SubsamplingScaleImageView)findViewById(R.id.imageView);
gifView = (GifImageView) findViewById(R.id.gifView);
setTitle(bi.getFile());
// imageView.setOnClickListener(new View.OnClickListener() {
// new TaskDownloadFile().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
downloadFile();
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
File baiDir = new File(Environment.getExternalStorageDirectory().getPath() + "/Bai/");
if (!baiDir.exists()){
baiDir.mkdir();
}
if (item.getItemId() == R.id.menu_save_img){
File to = new File(Environment.getExternalStorageDirectory().getPath() + "/Bai/" + bi.getFile());
try{
InputStream in = new FileInputStream(imagePath);
OutputStream out = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(getApplicationContext(), bi.getFile() + " guardado.", Toast.LENGTH_LONG).show();
}catch (Exception e){
e.printStackTrace();
}
}
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_viewer, menu);
return true;
}
private void downloadFile(){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("src", Context.MODE_PRIVATE);
final File filePath = new File(directory, bi.getParentBoard().getBoardDir() + "_" + bi.getFile());
final ProgressBar downloadBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
if (filePath.exists()){
downloadBar.setVisibility(View.GONE);
if (bi.getFile().endsWith(".gif")){
try {
GifDrawable gifFromFile = new GifDrawable(filePath);
gifView.setImageDrawable(gifFromFile);
imageView.setVisibility(View.GONE);
}catch(Exception e){
e.printStackTrace();
}
}else{
imageView.setImage(ImageSource.uri(filePath.toURI().getPath()));
gifView.setVisibility(View.GONE);
}
}
Ion.with(getApplicationContext())
.load("http://bienvenidoainternet.org/" + bi.getParentBoard().getBoardDir() + "/src/" + bi.getFile())
.progressBar(downloadBar)
.asInputStream()
.setCallback(new FutureCallback<InputStream>() {
@Override
public void onCompleted(Exception e, InputStream result) {
downloadBar.setVisibility(View.GONE);
if (e != null){
e.printStackTrace();
}else{
FileOutputStream fout;
try {
fout = new FileOutputStream(filePath);
final byte data[] = new byte[1024];
int count;
while ((count = result.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
}catch(Exception e1) {
e1.printStackTrace();
}
if (bi.getFile().endsWith(".gif")){
try {
GifDrawable gifFromFile = new GifDrawable(filePath);
gifView.setImageDrawable(gifFromFile);
imageView.setVisibility(View.GONE);
}catch(Exception e2){
e2.printStackTrace();
}
}else{
imageView.setImage(ImageSource.uri(filePath.toURI().getPath()));
gifView.setVisibility(View.GONE);
}
}
}
});
}
class TaskDownloadFile extends AsyncTask<Void, Void, File> {
@Override
protected File doInBackground(Void... params) {
Bitmap downloadedBitmap = null;
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("src", Context.MODE_PRIVATE);
File mypath = new File(directory, bi.getParentBoard().getBoardDir() + "_" + bi.getFile());
if (mypath.exists()){
System.out.println("[Viewer] resource exist!");
return mypath;
}
try {
String sUrl = "http://bienvenidoainternet.org/" + bi.getParentBoard().getBoardDir() + "/src/" + bi.getFile();
System.out.println("[Viewer]dwonloading " + sUrl);
// System.out.println(sUrl);
InputStream in = new java.net.URL(sUrl).openStream();
// downloadedBitmap = BitmapFactory.decodeStream(in);
// if (downloadedBitmap != null){
FileOutputStream fout = null;
try {
// in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(mypath);
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
// }
} catch (Exception e) {
e.printStackTrace();
}
return mypath;
}
@Override
protected void onPostExecute(File file) {
super.onPostExecute(file);
imagePath = file;
// iv.setImageBitmap(bitmap);
if (bi.getFile().endsWith(".gif")){
try {
GifDrawable gifFromFile = new GifDrawable(file);
gifView.setImageDrawable(gifFromFile);
imageView.setVisibility(View.GONE);
}catch(Exception e){
e.printStackTrace();
}
}else{
imageView.setImage(ImageSource.uri(file.toURI().getPath()));
gifView.setVisibility(View.GONE);
// imageView.setImage(ImageSource.resource(R.drawable.bai));
// System.out.println("not a gif file: " + file.toURI().getPath());
}
}
}
}
|