1 package mhr.app.fragments.toolbox;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
10 import mhr.app.AppMainActivity;
12 import mhr.appandroid.adapters.APDBitmap;
13 import mhr.appandroid.adapters.AndroidImageFile;
14 import mhr.appcore.AppCore;
15 import android.app.Activity;
16 import android.app.AlertDialog;
17 import android.app.Fragment;
18 import android.content.ActivityNotFoundException;
19 import android.content.DialogInterface;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.graphics.Bitmap.CompressFormat;
23 import android.media.MediaScannerConnection;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.os.Environment;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.AdapterView.OnItemSelectedListener;
34 import android.widget.Button;
35 import android.widget.EditText;
36 import android.widget.SeekBar;
37 import android.widget.SeekBar.OnSeekBarChangeListener;
38 import android.widget.Spinner;
39 import android.widget.TextView;
40 import android.widget.Toast;
53 protected View
root = null;
62 AlertDialog.Builder b =
new AlertDialog.Builder(
activity);
63 final View dv = inflater.inflate(
R.layout.dialog_export_image, null,
false);
64 ((Spinner)dv.findViewById(
R.id.ExportFormatSp)).setOnItemSelectedListener(
new OnItemSelectedListener() {
66 View quality = dv.findViewById(
R.id.ImageQualityValueGroup);
69 public void onItemSelected(AdapterView<?> parent, View view,
int position,
long id) {
71 quality.setVisibility(quality.VISIBLE);
73 quality.setVisibility(quality.INVISIBLE);
78 public void onNothingSelected(AdapterView<?> parent) {
83 ((SeekBar)dv.findViewById(
R.id.ImageQualityValueSB)).setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
85 TextView label = (TextView)dv.findViewById(
R.id.ImageQualityValueTV);
88 public void onStopTrackingTouch(SeekBar seekBar) {
93 public void onStartTrackingTouch(SeekBar seekBar) {
99 public void onProgressChanged(SeekBar seekBar,
int progress,
boolean fromUser) {
100 label.setText(Integer.toString(progress));
104 b.setMessage(
"Please select output file name, format and quality.").setTitle(
"Export image to gallery").setView(dv);
106 b.setPositiveButton(
"Export",
new DialogInterface.OnClickListener() {
109 final Spinner format = (Spinner)dv.findViewById(
R.id.ExportFormatSp);
110 final EditText name = (EditText) dv.findViewById(
R.id.ExportFileNameValueET);
111 final SeekBar quality = (SeekBar) dv.findViewById(
R.id.ImageQualityValueSB);
115 protected File getFile() {
116 File path = Environment.getExternalStoragePublicDirectory(
117 Environment.DIRECTORY_PICTURES);
118 return new File(path, name.getText().toString() + format.getSelectedItem().toString());
121 protected void writeFile(File f) {
126 OutputStream os = null;
128 f.getParentFile().mkdirs();
130 os =
new FileOutputStream(f);
131 switch (format.getSelectedItemPosition()) {
133 out.compress(CompressFormat.JPEG, quality.getProgress(), os);
136 out.compress(CompressFormat.PNG, 100, os);
143 }
catch (IOException e) {
149 }
catch (IOException e) {
155 MediaScannerConnection.scanFile(
activity,
new String[] { f.toString() }, null, null);
162 public void onClick(DialogInterface dialog,
int which) {
164 final File file = getFile();
167 AlertDialog.Builder b =
new AlertDialog.Builder(
activity);
168 b.setMessage(
"Do you want to overwrite the file?").setTitle(
"File already exists");
169 b.setPositiveButton(
"Overwrite",
new DialogInterface.OnClickListener() {
174 public void onClick(DialogInterface dialog,
int which) {
180 b.setNegativeButton(
"Cancel", null);
191 b.setNegativeButton(
"Cancel", null);
197 protected View
initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
198 View v = inflater.inflate(
R.layout.fragment_file_operations_tool_settings, container,
false);
200 root.findViewById(
R.id.CreateNewImageBtn).setOnClickListener(
this);
201 root.findViewById(
R.id.OpenGalleryImageBtn).setOnClickListener(
this);
202 root.findViewById(
R.id.OpenSavedImageBtn).setOnClickListener(
this);
203 root.findViewById(
R.id.SaveImageBtn).setOnClickListener(
this);
204 root.findViewById(
R.id.ExportImageBtn).setOnClickListener(
this);
221 case R.id.CreateNewImageBtn:
224 case R.id.OpenGalleryImageBtn:
225 Intent intent =
new Intent(Intent.ACTION_GET_CONTENT);
226 intent.setType(
"image/*");
227 intent.putExtra(Intent.EXTRA_TITLE,
"Open image with:");
228 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
231 }
catch (ActivityNotFoundException e) {
235 case R.id.OpenSavedImageBtn:
238 case R.id.SaveImageBtn:
241 case R.id.ExportImageBtn:
253 super.onAttach(activity);
272 public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
283 return initView(inflater, container, savedInstanceState);