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 Bitmap imageToUpload;
private Uri imageToUploadUri;
private Thread uploadThread;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -141,7 +142,10 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
cancelUpload.setOnClickListener(new OnClickListener() {
@Override
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);
textLayout.setVisibility(View.GONE);
new Thread(new Runnable() {
uploadThread = new Thread(new Runnable() {
@Override
public void run() {
Bitmap bm = null;
String url = null;
if (!uploadThread.isInterrupted()) {
if (filePath != null) {
bm = BitmapFactory.decodeFile(filePath);
if (bm != null && size != ImageSize.REAL) {
@ -550,31 +557,39 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else if (image != null) {
bm = image;
}
final Bitmap fbm = bm;
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
if (bm != null) {
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.delete();
}
if (!uploadThread.isInterrupted()) {
final Bitmap fbm = bm;
final String furl = url;
mHandler.post(new Runnable() {
@Override
public void run() {
uploadLayout.setVisibility(View.GONE);
textLayout.setVisibility(View.VISIBLE);
if (url != null) {
sendImageMessage(url, fbm);
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