Added cancel function for image upload

This commit is contained in:
Sylvain Berfini 2012-09-18 15:11:37 +02:00
parent 167ba0d174
commit f4e7fe1d25

View file

@ -104,6 +104,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private String fileToUploadPath; private String fileToUploadPath;
private Bitmap imageToUpload; private Bitmap imageToUpload;
private Uri imageToUploadUri; private Uri imageToUploadUri;
private Thread uploadThread;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -141,7 +142,10 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
cancelUpload.setOnClickListener(new OnClickListener() { cancelUpload.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
//TODO uploadThread.interrupt();
uploadLayout.setVisibility(View.GONE);
textLayout.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
} }
}); });
@ -533,48 +537,59 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
uploadLayout.setVisibility(View.VISIBLE); uploadLayout.setVisibility(View.VISIBLE);
textLayout.setVisibility(View.GONE); textLayout.setVisibility(View.GONE);
new Thread(new Runnable() { uploadThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Bitmap bm = null; Bitmap bm = null;
if (filePath != null) { String url = null;
bm = BitmapFactory.decodeFile(filePath);
if (bm != null && size != ImageSize.REAL) { if (!uploadThread.isInterrupted()) {
int pixelsMax = size == ImageSize.SMALL ? SIZE_SMALL : size == ImageSize.MEDIUM ? SIZE_MEDIUM : SIZE_LARGE; if (filePath != null) {
if (bm.getWidth() > bm.getHeight() && bm.getWidth() > pixelsMax) { bm = BitmapFactory.decodeFile(filePath);
bm = Bitmap.createScaledBitmap(bm, pixelsMax, (pixelsMax * bm.getHeight()) / bm.getWidth(), false); if (bm != null && size != ImageSize.REAL) {
} else if (bm.getHeight() > bm.getWidth() && bm.getHeight() > pixelsMax) { int pixelsMax = size == ImageSize.SMALL ? SIZE_SMALL : size == ImageSize.MEDIUM ? SIZE_MEDIUM : SIZE_LARGE;
bm = Bitmap.createScaledBitmap(bm, (pixelsMax * bm.getWidth()) / bm.getHeight(), pixelsMax, false); if (bm.getWidth() > bm.getHeight() && bm.getWidth() > pixelsMax) {
} bm = Bitmap.createScaledBitmap(bm, pixelsMax, (pixelsMax * bm.getHeight()) / bm.getWidth(), false);
} } else if (bm.getHeight() > bm.getWidth() && bm.getHeight() > pixelsMax) {
} else if (image != null) { bm = Bitmap.createScaledBitmap(bm, (pixelsMax * bm.getWidth()) / bm.getHeight(), pixelsMax, false);
bm = image; }
}
} else if (image != null) {
bm = image;
}
} }
final Bitmap fbm = bm;
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
if (bm != null) { if (bm != null) {
bm.compress(CompressFormat.JPEG, COMPRESSOR_QUALITY, outStream); bm.compress(CompressFormat.JPEG, COMPRESSOR_QUALITY, outStream);
} }
final String url = uploadImage(filePath, bm, COMPRESSOR_QUALITY, outStream.size()); if (!uploadThread.isInterrupted() && bm != null) {
File file = new File(Environment.getExternalStorageDirectory(), "linphone-android-photo-temp.jpg"); url = uploadImage(filePath, bm, COMPRESSOR_QUALITY, outStream.size());
file.delete(); File file = new File(Environment.getExternalStorageDirectory(), "linphone-android-photo-temp.jpg");
file.delete();
}
mHandler.post(new Runnable() { if (!uploadThread.isInterrupted()) {
@Override final Bitmap fbm = bm;
public void run() { final String furl = url;
uploadLayout.setVisibility(View.GONE); mHandler.post(new Runnable() {
textLayout.setVisibility(View.VISIBLE); @Override
if (url != null) { public void run() {
sendImageMessage(url, fbm); uploadLayout.setVisibility(View.GONE);
} else { textLayout.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_LONG).show(); progressBar.setProgress(0);
} if (furl != null) {
} sendImageMessage(furl, fbm);
}); } else {
Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_LONG).show();
}
}
});
}
} }
}).start(); });
uploadThread.start();
} }
@Override @Override