added button to open images

This commit is contained in:
Daniel Gultsch 2014-04-05 21:06:10 +02:00
parent 7749103909
commit 9fc7a1b980
4 changed files with 117 additions and 12 deletions

View file

@ -0,0 +1,76 @@
package eu.siacs.conversations.persistance;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import eu.siacs.conversations.entities.Conversation;
public class FileBackend {
private static int IMAGE_SIZE = 1920;
private Context context;
public FileBackend(Context context) {
this.context = context;
}
public File copyImageToPrivateStorage(Conversation conversation, Uri image) {
try {
InputStream is = context.getContentResolver().openInputStream(image);
String prefix = context.getFilesDir().getAbsolutePath();
String path = prefix+"/"+conversation.getAccount().getJid()+"/"+conversation.getContactJid();
String filename =new BigInteger(""+System.currentTimeMillis()).toString(32) + ".webp";
File file = new File(path+"/"+filename);
file.getParentFile().mkdirs();
file.createNewFile();
OutputStream os = new FileOutputStream(file);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
is.close();
int w = originalBitmap.getWidth();
int h = originalBitmap.getHeight();
boolean success;
if (Math.max(w, h) > IMAGE_SIZE) {
int scalledW;
int scalledH;
if (w<=h) {
scalledW = (int) (w / ((double) h/IMAGE_SIZE));
scalledH = IMAGE_SIZE;
} else {
scalledW = IMAGE_SIZE;
scalledH = (int) (h / ((double) w/IMAGE_SIZE));
}
Bitmap scalledBitmap = Bitmap.createScaledBitmap(originalBitmap, scalledW,scalledH, true);
success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP, 75, os);
} else {
success = originalBitmap.compress(Bitmap.CompressFormat.WEBP, 75, os);
}
if (!success) {
Log.d("xmppService","couldnt compress");
}
os.close();
return file;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

View file

@ -27,6 +27,7 @@ import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.persistance.OnPhoneContactsMerged;
import eu.siacs.conversations.ui.OnAccountListChangedListener;
import eu.siacs.conversations.ui.OnConversationListChangedListener;
@ -73,6 +74,7 @@ public class XmppConnectionService extends Service {
protected static final String LOGTAG = "xmppService";
public DatabaseBackend databaseBackend;
private FileBackend fileBackend;
public long startDate;
@ -381,6 +383,11 @@ public class XmppConnectionService extends Service {
}
public FileBackend getFileBackend() {
return this.fileBackend;
}
protected Conversation findMuc(String name, Account account) {
for (Conversation conversation : this.conversations) {
if (conversation.getContactJid().split("/")[0].equals(name)
@ -522,7 +529,8 @@ public class XmppConnectionService extends Service {
@Override
public void onCreate() {
ExceptionHelper.init(getApplicationContext());
databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
this.fileBackend = new FileBackend(getApplicationContext());
this.accounts = databaseBackend.getAccounts();
this.getConversations();

View file

@ -1,5 +1,8 @@
package eu.siacs.conversations.ui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@ -8,8 +11,11 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.utils.UIHelper;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.AlertDialog;
@ -18,6 +24,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.widget.SlidingPaneLayout;
@ -46,6 +53,7 @@ public class ConversationActivity extends XmppActivity {
public static final int REQUEST_SEND_MESSAGE = 0x75441;
public static final int REQUEST_DECRYPT_PGP = 0x76783;
private static final int ATTACH_FILE = 0x48502;
protected SlidingPaneLayout spl;
@ -272,6 +280,13 @@ public class ConversationActivity extends XmppActivity {
case android.R.id.home:
spl.openPane();
break;
case R.id.action_attach_file:
Intent attachFileIntent = new Intent();
attachFileIntent.setType("image/*");
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(attachFileIntent,
"Attach File"), ATTACH_FILE);
break;
case R.id.action_add:
startActivity(new Intent(this, ContactsActivity.class));
break;
@ -478,6 +493,11 @@ public class ConversationActivity extends XmppActivity {
if (selectedFragment!=null) {
selectedFragment.hidePgpPassphraseBox();
}
} else if (requestCode == ATTACH_FILE) {
FileBackend backend = xmppConnectionService.getFileBackend();
File file = backend.copyImageToPrivateStorage(getSelectedConversation(), data.getData());
Log.d(LOGTAG,"new file"+file.getAbsolutePath());
}
}
}

View file

@ -13,18 +13,19 @@ import android.os.Bundle;
import android.os.Looper;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Profile;
import android.provider.MediaStore;
public class PhoneHelper {
public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) {
public static void loadPhoneContacts(Context context,
final OnPhoneContactsLoadedListener listener) {
if (Looper.myLooper() == null) {
Looper.prepare();
}
final Looper mLooper = Looper.myLooper();
final Hashtable<String, Bundle> phoneContacts = new Hashtable<String, Bundle>();
final String[] PROJECTION = new String[] {
ContactsContract.Data._ID,
final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_THUMBNAIL_URI,
ContactsContract.Data.LOOKUP_KEY,