Search in sources :

Example 86 with Bitmap

use of android.graphics.Bitmap in project Talon-for-Twitter by klinker24.

the class DirectMessageConversation method getBitmapToSend.

private Bitmap getBitmapToSend(Uri uri) throws IOException {
    InputStream input = getContentResolver().openInputStream(uri);
    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    //optional
    onlyBoundsOptions.inDither = true;
    //optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();
    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;
    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
    double ratio = (originalSize > 1000) ? (originalSize / 1000) : 1.0;
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    //optional
    bitmapOptions.inDither = true;
    //optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    input = this.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    input.close();
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) BitmapFactory(android.graphics.BitmapFactory) Point(android.graphics.Point)

Example 87 with Bitmap

use of android.graphics.Bitmap in project Talon-for-Twitter by klinker24.

the class SendTweet method rotateBitmap.

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
    Log.v("talon_composing_image", "rotation: " + orientation);
    try {
        Matrix matrix = new Matrix();
        switch(orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) IOException(java.io.IOException)

Example 88 with Bitmap

use of android.graphics.Bitmap in project Talon-for-Twitter by klinker24.

the class TalonPullNotificationService method downloadImages.

public void downloadImages(Status status) {
    String profilePic = status.getUser().getBiggerProfileImageURL();
    String imageUrl = TweetLinkUtils.getLinksInStatus(status)[1];
    CacheableBitmapDrawable wrapper = null;
    try {
        wrapper = mCache.get(profilePic);
    } catch (OutOfMemoryError e) {
    }
    if (wrapper == null) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(profilePic).openConnection();
            InputStream is = new BufferedInputStream(conn.getInputStream());
            Bitmap image = decodeSampledBitmapFromResourceMemOpt(is, 500, 500);
            try {
                is.close();
            } catch (Exception e) {
            }
            try {
                conn.disconnect();
            } catch (Exception e) {
            }
            if (settings.roundContactImages) {
                image = ImageUtils.getCircle(image, this);
            }
            mCache.put(profilePic, image);
        } catch (Throwable e) {
        }
    }
    if (!imageUrl.equals("")) {
        try {
            wrapper = mCache.get(imageUrl);
        } catch (OutOfMemoryError e) {
            wrapper = null;
        }
        if (wrapper == null) {
            try {
                if (!imageUrl.contains(" ")) {
                    HttpURLConnection conn = (HttpURLConnection) new URL(imageUrl).openConnection();
                    InputStream is = new BufferedInputStream(conn.getInputStream());
                    Bitmap image = decodeSampledBitmapFromResourceMemOpt(is, 1000, 1000);
                    try {
                        is.close();
                    } catch (Exception e) {
                    }
                    try {
                        conn.disconnect();
                    } catch (Exception e) {
                    }
                    mCache.put(imageUrl, image);
                } else {
                    String[] pics = imageUrl.split(" ");
                    Bitmap[] bitmaps = new Bitmap[pics.length];
                    // need to download all of them, then combine them
                    for (int i = 0; i < pics.length; i++) {
                        String s = pics[i];
                        // The bitmap isn't cached so download from the web
                        HttpURLConnection conn = (HttpURLConnection) new URL(s).openConnection();
                        InputStream is = new BufferedInputStream(conn.getInputStream());
                        Bitmap b = decodeSampledBitmapFromResourceMemOpt(is, 1000, 1000);
                        try {
                            is.close();
                        } catch (Exception e) {
                        }
                        try {
                            conn.disconnect();
                        } catch (Exception e) {
                        }
                        // Add to cache
                        try {
                            mCache.put(s, b);
                            // throw it into our bitmap array for later
                            bitmaps[i] = b;
                        } catch (Exception e) {
                        }
                    }
                    // now that we have all of them, we need to put them together
                    Bitmap combined = ImageUtils.combineBitmaps(this, bitmaps);
                    try {
                        mCache.put(imageUrl, combined);
                    } catch (Exception e) {
                    }
                }
            } catch (Throwable e) {
            }
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) URL(java.net.URL) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable)

Example 89 with Bitmap

use of android.graphics.Bitmap in project Talon-for-Twitter by klinker24.

the class TweetWearableService method onMessageReceived.

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    final WearableUtils wearableUtils = new WearableUtils();
    final BitmapLruCache cache = App.getInstance(this).getBitmapCache();
    if (markReadHandler == null) {
        markReadHandler = new Handler();
    }
    final String message = new String(messageEvent.getData());
    Log.d(TAG, "got message: " + message);
    final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to GoogleApiClient.");
        return;
    }
    if (message.equals(KeyProperties.GET_DATA_MESSAGE)) {
        AppSettings settings = AppSettings.getInstance(this);
        Cursor tweets = HomeDataSource.getInstance(this).getWearCursor(settings.currentAccount);
        PutDataMapRequest dataMap = PutDataMapRequest.create(KeyProperties.PATH);
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> screennames = new ArrayList<String>();
        ArrayList<String> bodies = new ArrayList<String>();
        ArrayList<String> ids = new ArrayList<String>();
        if (tweets != null && tweets.moveToLast()) {
            do {
                String name = tweets.getString(tweets.getColumnIndex(HomeSQLiteHelper.COLUMN_NAME));
                String screenname = tweets.getString(tweets.getColumnIndex(HomeSQLiteHelper.COLUMN_SCREEN_NAME));
                String pic = tweets.getString(tweets.getColumnIndex(HomeSQLiteHelper.COLUMN_PRO_PIC));
                String body = tweets.getString(tweets.getColumnIndex(HomeSQLiteHelper.COLUMN_TEXT));
                long id = tweets.getLong(tweets.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID));
                String retweeter;
                try {
                    retweeter = tweets.getString(tweets.getColumnIndex(HomeSQLiteHelper.COLUMN_RETWEETER));
                } catch (Exception e) {
                    retweeter = "";
                }
                screennames.add(screenname);
                names.add(name);
                if (TextUtils.isEmpty(retweeter)) {
                    body = pic + KeyProperties.DIVIDER + body + KeyProperties.DIVIDER;
                } else {
                    body = pic + KeyProperties.DIVIDER + body + "<br><br>" + getString(R.string.retweeter) + retweeter + KeyProperties.DIVIDER;
                }
                bodies.add(Html.fromHtml(body.replace("<p>", KeyProperties.LINE_BREAK)).toString());
                ids.add(id + "");
            } while (tweets.moveToPrevious() && tweets.getCount() - tweets.getPosition() < MAX_ARTICLES_TO_SYNC);
            tweets.close();
        }
        dataMap.getDataMap().putStringArrayList(KeyProperties.KEY_USER_NAME, names);
        dataMap.getDataMap().putStringArrayList(KeyProperties.KEY_USER_SCREENNAME, screennames);
        dataMap.getDataMap().putStringArrayList(KeyProperties.KEY_TWEET, bodies);
        dataMap.getDataMap().putStringArrayList(KeyProperties.KEY_ID, ids);
        // light background with orange accent or theme color accent
        dataMap.getDataMap().putInt(KeyProperties.KEY_PRIMARY_COLOR, Color.parseColor("#dddddd"));
        if (settings.addonTheme) {
            dataMap.getDataMap().putInt(KeyProperties.KEY_ACCENT_COLOR, settings.accentInt);
        } else {
            dataMap.getDataMap().putInt(KeyProperties.KEY_ACCENT_COLOR, getResources().getColor(R.color.orange_primary_color));
        }
        dataMap.getDataMap().putLong(KeyProperties.KEY_DATE, System.currentTimeMillis());
        for (String node : wearableUtils.getNodes(googleApiClient)) {
            byte[] bytes = dataMap.asPutDataRequest().getData();
            Wearable.MessageApi.sendMessage(googleApiClient, node, KeyProperties.PATH, bytes);
            Log.v(TAG, "sent " + bytes.length + " bytes of data to node " + node);
        }
    } else if (message.startsWith(KeyProperties.MARK_READ_MESSAGE)) {
        markReadHandler.removeCallbacksAndMessages(null);
        markReadHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                String[] messageContent = message.split(KeyProperties.DIVIDER);
                final long id = Long.parseLong(messageContent[1]);
                final AppSettings settings = AppSettings.getInstance(TweetWearableService.this);
                try {
                    HomeDataSource.getInstance(TweetWearableService.this).markPosition(settings.currentAccount, id);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
                sendBroadcast(new Intent("com.klinker.android.twitter.CLEAR_PULL_UNREAD"));
                final SharedPreferences sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
                // mark tweetmarker if they use it
                if (AppSettings.getInstance(TweetWearableService.this).tweetmarker) {
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            TweetMarkerHelper helper = new TweetMarkerHelper(settings.currentAccount, sharedPrefs.getString("twitter_screen_name_" + settings.currentAccount, ""), Utils.getTwitter(TweetWearableService.this, settings), sharedPrefs);
                            helper.sendCurrentId("timeline", id);
                            startService(new Intent(TweetWearableService.this, HandleScrollService.class));
                        }
                    }).start();
                } else {
                    startService(new Intent(TweetWearableService.this, HandleScrollService.class));
                }
            }
        }, 5000);
    } else if (message.startsWith(KeyProperties.REQUEST_FAVORITE)) {
        final long tweetId = Long.parseLong(message.split(KeyProperties.DIVIDER)[1]);
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Utils.getTwitter(TweetWearableService.this, AppSettings.getInstance(TweetWearableService.this)).createFavorite(tweetId);
                } catch (Exception e) {
                }
            }
        }).start();
    } else if (message.startsWith(KeyProperties.REQUEST_COMPOSE)) {
        final String status = message.split(KeyProperties.DIVIDER)[1];
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Utils.getTwitter(TweetWearableService.this, AppSettings.getInstance(TweetWearableService.this)).updateStatus(status);
                } catch (Exception e) {
                }
            }
        }).start();
    } else if (message.startsWith(KeyProperties.REQUEST_RETWEET)) {
        final long tweetId = Long.parseLong(message.split(KeyProperties.DIVIDER)[1]);
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Utils.getTwitter(TweetWearableService.this, AppSettings.getInstance(TweetWearableService.this)).retweetStatus(tweetId);
                } catch (Exception e) {
                }
            }
        }).start();
    } else if (message.startsWith(KeyProperties.REQUEST_REPLY)) {
        final String tweet = message.split(KeyProperties.DIVIDER)[1];
        final long replyToId = Long.parseLong(message.split(KeyProperties.DIVIDER)[2]);
        final StatusUpdate status = new StatusUpdate(tweet);
        status.setInReplyToStatusId(replyToId);
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Utils.getTwitter(TweetWearableService.this, AppSettings.getInstance(TweetWearableService.this)).updateStatus(status);
                } catch (Exception e) {
                }
            }
        }).start();
    } else if (message.startsWith(KeyProperties.REQUEST_IMAGE)) {
        final String url = message.split(KeyProperties.DIVIDER)[1];
        Bitmap image = null;
        try {
            cache.get(url).getBitmap();
        } catch (Exception e) {
        }
        if (image != null) {
            image = adjustImage(image);
            sendImage(image, url, wearableUtils, googleApiClient);
        } else {
            // download it
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
                        InputStream is = new BufferedInputStream(conn.getInputStream());
                        Bitmap image = decodeSampledBitmapFromResourceMemOpt(is, 500, 500);
                        try {
                            is.close();
                        } catch (Exception e) {
                        }
                        try {
                            conn.disconnect();
                        } catch (Exception e) {
                        }
                        cache.put(url, image);
                        image = adjustImage(image);
                        sendImage(image, url, wearableUtils, googleApiClient);
                    } catch (Exception e) {
                    }
                }
            }).start();
        }
    } else {
        Log.e(TAG, "message not recognized");
    }
}
Also used : WearableUtils(com.klinker.android.twitter.utils.WearableUtils) AppSettings(com.klinker.android.twitter.settings.AppSettings) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) URL(java.net.URL) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) PutDataMapRequest(com.google.android.gms.wearable.PutDataMapRequest) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) SharedPreferences(android.content.SharedPreferences) TweetMarkerHelper(com.klinker.android.twitter.utils.api_helper.TweetMarkerHelper) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Handler(android.os.Handler) Intent(android.content.Intent) StatusUpdate(twitter4j.StatusUpdate) BitmapLruCache(uk.co.senab.bitmapcache.BitmapLruCache) HandleScrollService(com.klinker.android.twitter.activities.launcher_page.HandleScrollService) ConnectionResult(com.google.android.gms.common.ConnectionResult)

Example 90 with Bitmap

use of android.graphics.Bitmap in project Talon-for-Twitter by klinker24.

the class SendTweet method sendTweet.

public boolean sendTweet(AppSettings settings, Context context) {
    try {
        Twitter twitter = getTwitter();
        if (remainingChars < 0 && !pwiccer) {
            // twitlonger goes here
            TwitLongerHelper helper = new TwitLongerHelper(message, twitter);
            helper.setInReplyToStatusId(tweetId);
            return helper.createPost() != 0;
        } else {
            twitter4j.StatusUpdate reply = new twitter4j.StatusUpdate(message);
            reply.setInReplyToStatusId(tweetId);
            if (!attachedUri.equals("")) {
                // context being the Activity pointer
                File outputDir = context.getCacheDir();
                File f = File.createTempFile("compose", "picture", outputDir);
                Bitmap bitmap = getBitmapToSend(Uri.parse(attachedUri), context);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                byte[] bitmapdata = bos.toByteArray();
                FileOutputStream fos = new FileOutputStream(f);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
                if (!settings.twitpic) {
                    reply.setMedia(f);
                    twitter.updateStatus(reply);
                    return true;
                } else {
                    TwitPicHelper helper = new TwitPicHelper(twitter, message, f, context);
                    helper.setInReplyToStatusId(tweetId);
                    return helper.createPost() != 0;
                }
            } else {
                // no picture
                twitter.updateStatus(reply);
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : Bitmap(android.graphics.Bitmap) TwitPicHelper(com.klinker.android.twitter.utils.api_helper.TwitPicHelper) FileOutputStream(java.io.FileOutputStream) Twitter(twitter4j.Twitter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) TwitLongerHelper(com.klinker.android.twitter.utils.api_helper.TwitLongerHelper) IOException(java.io.IOException)

Aggregations

Bitmap (android.graphics.Bitmap)3746 Canvas (android.graphics.Canvas)889 Paint (android.graphics.Paint)709 BitmapDrawable (android.graphics.drawable.BitmapDrawable)461 IOException (java.io.IOException)394 Rect (android.graphics.Rect)341 File (java.io.File)262 Test (org.junit.Test)255 Matrix (android.graphics.Matrix)254 Drawable (android.graphics.drawable.Drawable)243 BitmapFactory (android.graphics.BitmapFactory)240 View (android.view.View)225 ImageView (android.widget.ImageView)210 Intent (android.content.Intent)187 FileOutputStream (java.io.FileOutputStream)186 InputStream (java.io.InputStream)171 RectF (android.graphics.RectF)150 FileNotFoundException (java.io.FileNotFoundException)150 Point (android.graphics.Point)148 Uri (android.net.Uri)128