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,10 +537,13 @@ 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;
String url = null;
if (!uploadThread.isInterrupted()) {
if (filePath != null) { if (filePath != null) {
bm = BitmapFactory.decodeFile(filePath); bm = BitmapFactory.decodeFile(filePath);
if (bm != null && size != ImageSize.REAL) { if (bm != null && size != ImageSize.REAL) {
@ -550,31 +557,39 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else if (image != null) { } else if (image != null) {
bm = image; 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) {
url = uploadImage(filePath, bm, COMPRESSOR_QUALITY, outStream.size());
File file = new File(Environment.getExternalStorageDirectory(), "linphone-android-photo-temp.jpg"); File file = new File(Environment.getExternalStorageDirectory(), "linphone-android-photo-temp.jpg");
file.delete(); file.delete();
}
if (!uploadThread.isInterrupted()) {
final Bitmap fbm = bm;
final String furl = url;
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
uploadLayout.setVisibility(View.GONE); uploadLayout.setVisibility(View.GONE);
textLayout.setVisibility(View.VISIBLE); textLayout.setVisibility(View.VISIBLE);
if (url != null) { progressBar.setProgress(0);
sendImageMessage(url, fbm); if (furl != null) {
sendImageMessage(furl, fbm);
} else { } else {
Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_LONG).show();
} }
} }
}); });
} }
}).start(); }
});
uploadThread.start();
} }
@Override @Override