0

My attempt does not work at all unfortunately. Weirdly enough, capturing photos from camera works when debugging but does not in production.

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (!Debug.isDebuggerConnected()){
        Debug.waitForDebugger();
        Log.d("debug", "started"); // Insert a breakpoint at this line!!
    }

    if (resultCode != RESULT_OK) {
        return;
    }

    File file = null;
    Uri path = null;

    Bitmap image = null;

    switch (requestCode) {
        case RequestCodes.REQUEST_IMAGE_CAPTURE:
            Bundle extras = data.getExtras();
            image = (Bitmap) extras.get("data");
            file = ImageUtils.saveToFile(image, "profile_picture", this);
            mProfileImageView.setImageBitmap(image);
            mCurrentAbsolutePath = file.getAbsolutePath();
            break;
        case RequestCodes.REQUEST_IMAGE_SELECT:
            path = data.getData();
            mProfileImageView.setImageURI(path);
            mCurrentAbsolutePath = path.getPath();
            file = new File(mCurrentAbsolutePath);
            image = BitmapFactory.decodeFile(mCurrentAbsolutePath, new BitmapFactory.Options());
            break;
        default:
            break;
    }

    try {
        if(RequestCodes.REQUEST_IMAGE_SELECT == requestCode){
            file = File.createTempFile(
                    "user_picture",  /* prefix */
                    ".jpeg",         /* suffix */
                    getExternalFilesDir(Environment.DIRECTORY_PICTURES)      /* directory */
            );
            File pathFile = new File(ImageUtils.getPath(path, this));
            GeneralUtils.copy(pathFile, file);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, 100, 100);
    String thumbnailPath = null;

    // Edited source: https://stackoverflow.com/a/673014/6519101
    FileOutputStream out = null;
    try {
        // PNG is a lossless format, the compression factor (100) is ignored
        thumbnailPath = File.createTempFile(
                "user_picture_thumbnail",  /* prefix */
                ".png",         /* suffix */
                getExternalFilesDir(Environment.DIRECTORY_PICTURES)      /* directory */
        ).getAbsolutePath();
        out = new FileOutputStream(thumbnailPath);
        thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    String finalPath = file.getPath();
    UserClient client = new UserClient();
    String finalThumbnailPath = thumbnailPath;
    client.changeUserPicture(file, FileUtils.getMimeType(this, Uri.fromFile(file)), new ApiListener<Response<ResponseBody>>(this){
        @Override
        public void onSuccess(Response<ResponseBody> response, int statusCode) {
            SharedPreferencesManager preferences = SharedPreferencesManager.getInstance();
            preferences.put(SharedPreferencesManager.Key.ACCOUNT_IMAGE_PATH, finalPath);
            preferences.put(SharedPreferencesManager.Key.ACCOUNT_IMAGE_THUMBNAIL_PATH, finalThumbnailPath);
            super.onSuccess(response, statusCode);
        }
    });
}

Unfortunately when debugging the from example of a path "/0/4/content://media/external/images/media/54257/ORIGINAL/NONE/1043890606" decoded file end up being null and breaks everything.

What is the best way of both getting from gallery and capturing image from photo?

Necroqubus
  • 331
  • 2
  • 18

1 Answers1

1

What you should be using are content providers and resolvers here which can be thought of as databases and accessing databases for easier understanding.

That path you have there is called a URI, which is essentially like a link to a database entry. Both the camera and gallery uses content providers and resolvers actively. When a photo is taken, it is saved but the camera app also lets content provider know a new entry has been added. Now every app who has the content resolvers, such as the gallery app, can find that photo because the URI exist.

So you should be following the guides to implement content resolver if you want to access all photos in gallery.

As an aside, if you use code to copy an image file but does up update the content providers, your other app cannot see that new copied file unless it knows the absolute path. But when you restart your phone, some system does a full recheck for all image files and your content provider could be updated with the newly copied file. So try restarting your phone when testing.

Bqin1
  • 467
  • 1
  • 9
  • 19