Example usage for android.os Bundle putByteArray

List of usage examples for android.os Bundle putByteArray

Introduction

In this page you can find the example usage for android.os Bundle putByteArray.

Prototype

@Override
public void putByteArray(@Nullable String key, @Nullable byte[] value) 

Source Link

Document

Inserts a byte array value into the mapping of this Bundle, replacing any existing value for the given key.

Usage

From source file:com.wareninja.android.opensource.oauth2login.common.Utils.java

/**
 * Connect to an HTTP URL and return the response as a string.
 * /*  w  w w.  j a  v a2  s  . com*/
 * Note that the HTTP method override is used on non-GET requests. (i.e.
 * requests are made as "POST" with method specified in the body).
 * 
 * @param url - the resource to open: must be a welformed URL
 * @param method - the HTTP method to use ("GET", "POST", etc.)
 * @param params - the query parameter for the URL (e.g. access_token=foo)
 * @return the URL contents as a String
 * @throws MalformedURLException - if the URL format is invalid
 * @throws IOException - if a network problem occurs
 */
public static String openUrl(String url, String method, Bundle params)
        throws MalformedURLException, IOException {
    // random string as boundary for multi-part http post
    String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
    String endLine = "\r\n";

    OutputStream os;

    if (method.equals("GET")) {
        url = url + "?" + encodeUrl(params);
    }
    if (AppContext.DEBUG)
        Log.d("Facebook-Util", method + " URL: " + url);
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent",
            System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK");
    if (!method.equals("GET")) {
        Bundle dataparams = new Bundle();
        for (String key : params.keySet()) {

            /*
             if (params.getByteArray(key) != null) {
                dataparams.putByteArray(key, params.getByteArray(key));
             }
             */
            // YG: added this to avoid fups
            byte[] byteArr = null;
            try {
                byteArr = (byte[]) params.get(key);
            } catch (Exception ex1) {
            }
            if (byteArr != null)
                dataparams.putByteArray(key, byteArr);
        }

        // use method override
        if (!params.containsKey("method")) {
            params.putString("method", method);
        }

        if (params.containsKey("access_token")) {
            String decoded_token = URLDecoder.decode(params.getString("access_token"));
            params.putString("access_token", decoded_token);
        }

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + strBoundary);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.connect();
        os = new BufferedOutputStream(conn.getOutputStream());

        os.write(("--" + strBoundary + endLine).getBytes());
        os.write((encodePostBody(params, strBoundary)).getBytes());
        os.write((endLine + "--" + strBoundary + endLine).getBytes());

        if (!dataparams.isEmpty()) {

            for (String key : dataparams.keySet()) {
                os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
                os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
                os.write(dataparams.getByteArray(key));
                os.write((endLine + "--" + strBoundary + endLine).getBytes());

            }
        }
        os.flush();
    }

    String response = "";
    try {
        response = read(conn.getInputStream());
    } catch (FileNotFoundException e) {
        // Error Stream contains JSON that we can parse to a FB error
        response = read(conn.getErrorStream());
    }
    if (AppContext.DEBUG)
        Log.d("Facebook-Util", method + " response: " + response);

    return response;
}

From source file:com.facebook.internal.Utility.java

public static void putObjectInBundle(Bundle bundle, String key, Object value) {
    if (value instanceof String) {
        bundle.putString(key, (String) value);
    } else if (value instanceof Parcelable) {
        bundle.putParcelable(key, (Parcelable) value);
    } else if (value instanceof byte[]) {
        bundle.putByteArray(key, (byte[]) value);
    } else {/*  ww w  . jav a2  s  . com*/
        throw new FacebookException("attempted to add unsupported type to Bundle");
    }
}

From source file:cn.edu.szjm.service.PostRecordTask.java

protected Integer doInBackground(Object... params) {

    requestListener.onRequestStart();/*from   w ww.  j  a  v  a2  s.co m*/

    if ((activity == null) || (!(in == null) && (news == null)))
        requestListener.onRequestFault(new Throwable(new NullPointerException().getMessage()));

    Kaixin kaixin = Kaixin.getInstance();

    try {

        Bundle bundle = new Bundle();
        bundle.putByteArray("content", news.getBytes());

        Map<String, Object> photoes = new HashMap<String, Object>();
        photoes.put("filename", in);

        String jsonResult = kaixin.uploadContent(activity, RESTAPI_INTERFACE_POSTRECORD, bundle, photoes);

        if (TextUtils.isEmpty(jsonResult)) {

            requestListener.onRequestFault(new KaixinError(Constant.RESULT_FAILED_REQUEST_ERR,
                    "/", "", ""));

        } else {

            KaixinError kaixinError = Util.parseRequestError(jsonResult);

            if (kaixinError != null) {

                requestListener.onRequestError(kaixinError);

            } else {

                long rid = getRecordID(jsonResult);

                if (rid < 0)
                    requestListener.onRequestError(new KaixinError(Constant.RESULT_POST_RECORD_FAILED,
                            "/???", "", jsonResult));
                else
                    requestListener.onRequestComplete(jsonResult);
            }

        }

    } catch (IOException e) {
        requestListener
                .onRequestFault(new KaixinError(Constant.RESULT_FAILED_NETWORK_ERR, e.getMessage(), "", ""));
    } catch (JSONException e) {
        requestListener
                .onRequestError(new KaixinError(Constant.RESULT_FAILED_JSON_PARSE_ERR, e.getMessage(), "", ""));
    }
    return params.length;
}

From source file:mobisocial.musubi.ui.fragments.ChooseImageDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity()).setTitle("Choose an Image...")
            .setItems(new String[] { "From Camera", "From Gallery" }, new DialogInterface.OnClickListener() {
                @SuppressWarnings("deprecation")
                @Override/*  w w  w. j av a  2s. c o  m*/
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                    case 0:
                        final Activity activity = getActivity();
                        Toast.makeText(activity, "Loading camera...", Toast.LENGTH_SHORT).show();
                        ((InstrumentedActivity) activity)
                                .doActivityForResult(new PhotoTaker(activity, new PhotoTaker.ResultHandler() {
                                    @Override
                                    public void onResult(Uri imageUri) {
                                        Log.d(getClass().getSimpleName(), "Updating thumbnail...");

                                        try {
                                            UriImage image = new UriImage(activity, imageUri);
                                            byte[] data = image.getResizedImageData(512, 512,
                                                    PictureObj.MAX_IMAGE_SIZE / 2);
                                            // profile
                                            Bitmap sourceBitmap = BitmapFactory.decodeByteArray(data, 0,
                                                    data.length);
                                            int width = sourceBitmap.getWidth();
                                            int height = sourceBitmap.getHeight();
                                            int cropSize = Math.min(width, height);
                                            Bitmap cropped = Bitmap.createBitmap(sourceBitmap, 0, 0, cropSize,
                                                    cropSize);
                                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                            cropped.compress(Bitmap.CompressFormat.JPEG, 90, baos);
                                            cropped.recycle();
                                            sourceBitmap.recycle();

                                            Bundle bundle = new Bundle();
                                            bundle.putByteArray(EXTRA_THUMBNAIL, baos.toByteArray());
                                            Intent res = new Intent();
                                            res.putExtras(bundle);
                                            getTargetFragment().onActivityResult(REQUEST_PROFILE_PICTURE,
                                                    Activity.RESULT_OK, res);
                                        } catch (Throwable t) {
                                            Log.e("ViewProfile", "failed to generate thumbnail of profile", t);
                                            Toast.makeText(activity,
                                                    "Profile picture capture failed.  Try again.",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }, 200, false));
                        break;
                    case 1:
                        Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
                        gallery.setType("image/*");
                        // god damn fragments.
                        getTargetFragment().startActivityForResult(Intent.createChooser(gallery, null),
                                REQUEST_GALLERY_THUMBNAIL);
                        break;
                    }
                }
            }).create();
}

From source file:com.quaninventory.scanner.activities.DrawerActivity.java

@Override
public void sendQRCode(Bitmap qrCode) {
    if (qrCode != null) {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        qrCode.compress(Bitmap.CompressFormat.PNG, 50, bs);
        QRView qrView = new QRView();

        Bundle b = new Bundle();
        b.putByteArray("BS", bs.toByteArray());
        qrView.setArguments(b);/*from  w ww  . j av  a2  s  . c  o  m*/

        qrView.show(getFragmentManager(), "qrCode");
    }
}

From source file:org.sufficientlysecure.keychain.ui.CreateKeyYubiKeyImportFragment.java

@Override
public void onSaveInstanceState(Bundle args) {
    super.onSaveInstanceState(args);

    args.putByteArray(ARG_FINGERPRINT, mNfcFingerprints);
    args.putByteArray(ARG_AID, mNfcAid);
    args.putString(ARG_USER_ID, mNfcUserId);
}

From source file:org.sufficientlysecure.keychain.ui.CreateYubiKeyImportResetFragment.java

@Override
public void onSaveInstanceState(Bundle args) {
    super.onSaveInstanceState(args);

    args.putByteArray(ARG_FINGERPRINTS, mNfcFingerprints);
    args.putByteArray(ARG_AID, mNfcAid);
    args.putString(ARG_USER_ID, mNfcUserId);
}

From source file:org.sufficientlysecure.keychain.ui.CreateSecurityTokenImportResetFragment.java

@Override
public void onSaveInstanceState(Bundle args) {
    super.onSaveInstanceState(args);

    args.putByteArray(ARG_FINGERPRINTS, mTokenFingerprints);
    args.putByteArray(ARG_AID, mTokenAid);
    args.putString(ARG_USER_ID, mTokenUserId);
}

From source file:de.persoapp.android.activity.AuthenticateActivity.java

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mPin != null) {
        outState.putByteArray(PIN_KEY, mPin);
    }//  w ww . j a  v  a2 s.c  o  m
    outState.putLong(RESULT_CHAT_KEY, mResultChat);
}

From source file:net.reichholf.dreamdroid.fragment.ScreenShotFragment.java

@Override
public void onSaveInstanceState(Bundle outState) {
    mScannerConn.disconnect();
    outState.putByteArray("rawImage", mRawImage);
}