{
AlertDialog.Builder b =
new AlertDialog.Builder(
activity);
final View dv = inflater.inflate(
R.layout.dialog_export_image, null,
false);
((Spinner)dv.findViewById(
R.id.ExportFormatSp)).setOnItemSelectedListener(
new OnItemSelectedListener() {
View quality = dv.findViewById(
R.id.ImageQualityValueGroup);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
quality.setVisibility(quality.VISIBLE);
} else {
quality.setVisibility(quality.INVISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
((SeekBar)dv.findViewById(
R.id.ImageQualityValueSB)).setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
TextView label = (TextView)dv.findViewById(
R.id.ImageQualityValueTV);
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
label.setText(Integer.toString(progress));
}
});
b.setMessage("Please select output file name, format and quality.").setTitle("Export image to gallery").setView(dv);
b.setPositiveButton("Export", new DialogInterface.OnClickListener() {
final Spinner format = (Spinner)dv.findViewById(
R.id.ExportFormatSp);
final EditText name = (EditText) dv.findViewById(
R.id.ExportFileNameValueET);
final SeekBar quality = (SeekBar) dv.findViewById(
R.id.ImageQualityValueSB);
boolean ow = false;
protected File getFile() {
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
return new File(path, name.getText().toString() + format.getSelectedItem().toString());
}
protected void writeFile(File f) {
Bitmap out = ((APDBitmap)c.getOutput()).getBitmap();
OutputStream os = null;
try {
f.getParentFile().mkdirs();
os = new FileOutputStream(f);
switch (format.getSelectedItemPosition()) {
case 0:
out.compress(CompressFormat.JPEG, quality.getProgress(), os);
break;
case 1:
out.compress(CompressFormat.PNG, 100, os);
break;
default:
break;
}
os.flush();
} catch (IOException e) {
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
}
MediaScannerConnection.scanFile(
activity,
new String[] { f.toString() }, null, null);
c.wake();
}
@Override
public void onClick(DialogInterface dialog,
int which) {
final File file = getFile();
if (file.exists()) {
AlertDialog.Builder b =
new AlertDialog.Builder(
activity);
b.setMessage("Do you want to overwrite the file?").setTitle("File already exists");
b.setPositiveButton("Overwrite", new DialogInterface.OnClickListener() {
File f = file;
@Override
public void onClick(DialogInterface dialog,
int which) {
writeFile(f);
}
});
b.setNegativeButton("Cancel", null);
b.create().show();
return;
} else {
writeFile(file);
}
}
});
b.setNegativeButton("Cancel", null);
}