Fix for black screen when trying to upload a large file

This commit is contained in:
Sylvain Berfini 2015-03-17 16:48:13 +01:00
parent 48a8ea1a70
commit 159e9ccfe5
2 changed files with 51 additions and 28 deletions

View file

@ -405,4 +405,5 @@
<string name="retry">Retry</string>
<string name="remote_provisioning_failure">Failed to download or apply remote provisioning profile...</string>
<string name="remote_provisioning_again_title">Remote provisioning</string>
<string name="remote_provisioning_again_message">Do you want to change the provisioning URI ?</string></resources>
<string name="remote_provisioning_again_message">Do you want to change the provisioning URI ?</string>
<string name="processing_image">Processing image, can take up to a few seconds depending on the size of the file</string></resources>

View file

@ -41,6 +41,7 @@ import org.linphone.ui.BubbleChat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
@ -49,8 +50,8 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
@ -101,7 +102,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private TextWatcher textWatcher;
private ViewTreeObserver.OnGlobalLayoutListener keyboardListener;
private ChatMessageAdapter adapter;
private Handler mHandler = new Handler();
private LinphoneCoreListenerBase mListener;
private ByteArrayOutputStream mDownloadedImageStream;
@ -497,24 +497,8 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
if (chatRoom != null && path != null && path.length() > 0 && isNetworkReachable) {
Bitmap bm = BitmapFactory.decodeFile(path);
if (bm != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
mUploadingImageStream = new ByteArrayInputStream(byteArray);
LinphoneContent content = LinphoneCoreFactory.instance().createLinphoneContent("image", "jpeg", byteArray, null);
String fileName = path.substring(path.lastIndexOf("/") + 1);
content.setName(fileName);
LinphoneChatMessage message = chatRoom.createFileTransferMessage(content);
message.setListener(this);
message.setAppData(path);
uploadLayout.setVisibility(View.VISIBLE);
textLayout.setVisibility(View.GONE);
chatRoom.sendChatMessage(message);
currentMessageInFileTransferUploadState = message;
FileUploadPrepareTask task = new FileUploadPrepareTask(getActivity(), path);
task.execute(bm);
} else {
Log.e("Error, bitmap factory can't read " + path);
}
@ -522,6 +506,50 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
LinphoneActivity.instance().displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG);
}
}
class FileUploadPrepareTask extends AsyncTask<Bitmap, Void, byte[]> {
private String path;
private ProgressDialog progressDialog;
public FileUploadPrepareTask(Context context, String fileToUploadPath) {
path = fileToUploadPath;
uploadLayout.setVisibility(View.VISIBLE);
textLayout.setVisibility(View.GONE);
progressDialog = new ProgressDialog(context);
progressDialog.setIndeterminate(true);
progressDialog.setMessage(getString(R.string.processing_image));
progressDialog.show();
}
@Override
protected byte[] doInBackground(Bitmap... params) {
Bitmap bm = params[0];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
@Override
protected void onPostExecute(byte[] result) {
progressDialog.dismiss();
mUploadingImageStream = new ByteArrayInputStream(result);
LinphoneContent content = LinphoneCoreFactory.instance().createLinphoneContent("image", "jpeg", result, null);
String fileName = path.substring(path.lastIndexOf("/") + 1);
content.setName(fileName);
LinphoneChatMessage message = chatRoom.createFileTransferMessage(content);
message.setListener(ChatFragment.this);
message.setAppData(path);
chatRoom.sendChatMessage(message);
currentMessageInFileTransferUploadState = message;
}
}
private LinphoneChatMessage getMessageForId(int id) {
for (LinphoneChatMessage message : chatRoom.getHistory()) {
@ -610,13 +638,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
}
if (fileToUploadPath != null) {
final String filePath = fileToUploadPath;
mHandler.post(new Runnable() {
@Override
public void run() {
sendImageMessage(filePath);
}
});
sendImageMessage(fileToUploadPath);
}
} else {
super.onActivityResult(requestCode, resultCode, data);