Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFile.

Prototype

public static Bitmap decodeFile(String pathName) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:at.zone.madeleine.slideshow.ImageDownloader.java

/**
 * Retrieve Bitmap from local storage.//  w ww .  ja  v a  2  s. c  o m
 */
private Bitmap getBitmapFromDisc(String url) {
    String filename = getFileNameFromURL(url);
    if (url.contains("thumbnail")) {
        filename = "thumbnail_".concat(filename);
    }
    String path = ContentManager.getInstance().getSlideshowPath();
    Bitmap bitmap = BitmapFactory.decodeFile(path.concat(filename));
    return bitmap;
}

From source file:com.dongfang.dicos.sina.UtilSina.java

public static String openUrl(Context context, String url, String method, WeiboParameters params, String file,
        Token token) throws WeiboException {
    String result = "";
    try {/*from  w w w .j a  va2  s.  c o m*/
        HttpClient client = getNewHttpClient(context);
        HttpUriRequest request = null;
        ByteArrayOutputStream bos = null;
        if (method.equals("GET")) {
            url = url + "?" + encodeUrl(params);
            HttpGet get = new HttpGet(url);
            request = get;
        } else if (method.equals("POST")) {
            HttpPost post = new HttpPost(url);
            byte[] data = null;
            bos = new ByteArrayOutputStream(1024 * 50);
            if (!TextUtils.isEmpty(file)) {
                ULog.d(tag, "openUrl !TextUtils.isEmpty(file)");
                UtilSina.paramToUpload(bos, params);
                post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
                Bitmap bf = BitmapFactory.decodeFile(file);

                UtilSina.imageContentToUpload(bos, bf);

            } else {
                ULog.d(tag, "openUrl ");
                post.setHeader("Content-Type", "application/x-www-form-urlencoded");
                String postParam = encodeParameters(params);
                data = postParam.getBytes("UTF-8");
                bos.write(data);
            }
            data = bos.toByteArray();
            bos.close();
            // UrlEncodedFormEntity entity = getPostParamters(params);
            ByteArrayEntity formEntity = new ByteArrayEntity(data);
            post.setEntity(formEntity);
            request = post;
        } else if (method.equals("DELETE")) {
            request = new HttpDelete(url);
        }
        setHeader(method, request, params, url, token);
        HttpResponse response = client.execute(request);
        for (Header h : request.getAllHeaders()) {
            ULog.e(tag, h.getName() + " -- " + h.getValue());
        }

        StatusLine status = response.getStatusLine();
        int statusCode = status.getStatusCode();

        if (statusCode != 200) {
            result = read(response);
            ULog.e("UtilSina", "result = " + result);
            // result = new String(result.getBytes("utf-8"),"gbk");
            // ULog.e("UtilSina", "result = " + result);
            String err = null;
            int errCode = 0;
            try {
                JSONObject json = new JSONObject(result);
                err = json.getString("error");
                errCode = json.getInt("error_code");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            throw new WeiboException(String.format(err), errCode);
        }
        // parse content stream from response
        result = read(response);
        return result;
    } catch (IOException e) {
        throw new WeiboException(e);
    }
}

From source file:edu.cloud.iot.reception.ocr.FaceRecognitionActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        Bitmap img = (Bitmap) data.getExtras().get("data");
        imageResult.setImageBitmap(img);
    }//from  w ww .j  a v a2  s.co m
    if (requestCode == SET_COMPARE_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        Bitmap compImg = BitmapFactory.decodeFile(picturePath);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        compImg.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        compareImageBytes = bos.toByteArray();

        // ImageView imageView = (ImageView) findViewById(R.id.imgView);
        // imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

From source file:com.expertiseandroid.lib.sociallib.connectors.FacebookConnector.java

/**
 * The albumId could be any facebook identifier that supports a photos
 * connection/*from  ww w . j a  va  2 s . co m*/
 * @throws JSONException 
 * 
 * @see Graph API
 */
public boolean sendPhoto(String filePath, String caption, String albumId) throws IOException, JSONException {
    Bitmap image = BitmapFactory.decodeFile(filePath);

    Map<String, String> params = new HashMap<String, String>();
    params.put(ACCESS_TOKEN, URLDecoder.decode(facebook.getAccessToken(), ENCODING));
    params.put(MESSAGE, caption);

    String url = GRAPH_URL + albumId + GET_PHOTOS;
    ReadableResponse response = new StringWrapper(Utils.postWithAttachment(url, params, image));
    return reader.readResponse(response);
}

From source file:com.nookdevs.market.NookMarket.java

private void loadWallpaper() {
    String wallPaperFile = getWallpaperFile();
    if (wallPaperFile != null) {
        try {/*from www . j  a  v a2s.co  m*/
            ImageView img = (ImageView) findViewById(R.id.mainimage);
            wallPaperFile = wallPaperFile.substring(7);
            Bitmap bMap = BitmapFactory.decodeFile(wallPaperFile);
            img.setImageBitmap(bMap);
        } catch (Exception ex) {
            Log.e(LOGTAG, ex.getMessage(), ex);
        }
    }
}

From source file:com.khoahuy.phototag.HomeActivity.java

private void galleryAddPic() {
    /*/*from ww w.j  av  a 2s .c  om*/
     * Intent mediaScanIntent = new Intent(
     * "android.intent.action.MEDIA_SCANNER_SCAN_FILE"); File f = new
     * File(mCurrentPhotoPath); Uri contentUri = Uri.fromFile(f);
     * mediaScanIntent.setData(contentUri);
     * this.sendBroadcast(mediaScanIntent);
     */

    // Resize image
    try {
        Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
        Bitmap bm2 = ImageUtils.getReduce4TimesBitmap(bm);
        FileOutputStream out = new FileOutputStream(mCurrentPhotoPath);
        bm2.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Store to db
    NFCItem item = new NFCItem();
    item.setNfcid(nfcid);
    item.setImage(mCurrentPhotoPath);
    item.setCheckIn(DateUtils.getCurrentTimestamp());
    nfcProvider2.addWaitingItem(item);
}

From source file:com.makotosan.vimeodroid.common.ImageDownloader.java

private Bitmap addBitmapToStorage(String url, InputStream input) {
    if (Methods.isExternalStorageWritable()) {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        String[] urlParts = url.split("/");
        path += "/Android/data/" + app.getPackageName() + "/cache/";
        OutputStream out = null;//  w  w  w.  j ava2 s.  c  om
        byte[] buffer = new byte[8 * 1024];
        try {
            final String fileName = path + urlParts[urlParts.length - 1] + ".vdtmp";
            File file = new File(path);
            file.mkdir();
            file = new File(fileName);
            out = new FileOutputStream(file);
            int bytesRead;
            while ((bytesRead = input.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }

            return BitmapFactory.decodeFile(fileName);
        } catch (Exception e) {
            // What to do here?
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                // Swallow exception
            }
        }
    } else {
        return BitmapFactory.decodeStream(input);
    }
    return null;
}

From source file:com.uzmap.pkg.uzmodules.UIMediaScanner.UIMediaScanner.java

public String createThumbPath(String orgPath) {

    int degree = BitmapToolkit.readPictureDegree(orgPath);
    Bitmap srcBitmap = BitmapFactory.decodeFile(orgPath);

    if (srcBitmap == null) {
        return null;
    }// www .jav a2 s  .  c o m

    Bitmap createdBitmap = ThumbnailUtils.extractThumbnail(srcBitmap, 157, 157);
    if (degree != 0) {
        createdBitmap = BitmapToolkit.rotaingImageView(degree, createdBitmap);
    }

    String realPathStr = SDCARD_PATH + "/DCIM/.thumbnails_for_me";
    File realPath = new File(realPathStr);
    if (!realPath.exists()) {
        realPath.mkdirs();
    }

    File imagePath = new File(realPath, Util.stringToMD5(orgPath) + ".jpg");

    /* replace the old ThunmbNail image */
    FileOutputStream outStream;
    try {
        outStream = new FileOutputStream(imagePath);
        createdBitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return imagePath.getAbsolutePath();
}

From source file:com.ritul.truckshare.RegisterActivity.DriverLicenseActivity.java

private void convertimagetobyte() {

    if (selectedpath.length() > 0) {
        String picturePath = selectedpath;
        int MAX_IMAGE_SIZE = 200 * 1024; // max final file size
        Bitmap bmpPic = BitmapFactory.decodeFile(picturePath);
        picturePath = saveToInternalSorage(bmpPic);
        bmpPic = BitmapFactory.decodeFile(picturePath);
        if ((bmpPic.getWidth() >= 100) && (bmpPic.getHeight() >= 100)) {
            BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
            bmpOptions.inSampleSize = 1;
            while ((bmpPic.getWidth() >= 100) && (bmpPic.getHeight() >= 100)) {
                bmpOptions.inSampleSize++;
                bmpPic = BitmapFactory.decodeFile(picturePath, bmpOptions);
            }// ww w.  jav a  2 s . co  m
            //Log.d(, "Resize: " + bmpOptions.inSampleSize);
        }
        int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
        int streamLength = MAX_IMAGE_SIZE;
        boolean _isComp = false;
        while (streamLength >= MAX_IMAGE_SIZE) {
            ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
            compressQuality -= 5;
            //Log.d(TAG, "Quality: " + compressQuality);
            bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            imageByte = bmpStream.toByteArray();
            streamLength = imageByte.length;
            _isComp = true;
            //  Log.d(TAG, "Size: " + streamLength);
        }
        if (!_isComp) {
            try {
                FileOutputStream bmpFile = new FileOutputStream(picturePath);
                ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
                bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
                imageByte = bmpStream.toByteArray();
                bmpFile.flush();
                bmpFile.close();
            } catch (Exception e) {
                // Log.e(TAG, "Error on saving file");
            }
        }
    }
}

From source file:com.makotosan.vimeodroid.common.ImageDownloader.java

private Bitmap getBitmapFromStorage(String url) {
    // Now try reading from the file system cache
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    String[] urlParts = url.split("/");
    path += "/Android/data/" + app.getPackageName() + "/cache/" + urlParts[urlParts.length - 1] + ".vdtmp";
    final Bitmap bitmap = BitmapFactory.decodeFile(path);
    if (bitmap != null) {
        return bitmap;
    }//w  w  w  . j  a v  a 2 s  . c o m

    return null;
}