java_lang_programming.com.android_media_demo.ImageSelectionCropDemo.java Source code

Java tutorial

Introduction

Here is the source code for java_lang_programming.com.android_media_demo.ImageSelectionCropDemo.java

Source

/**
 * Copyright (C) 2017 Programming Java Android Development Project
 * Programming Java is
 * <p>
 * http://java-lang-programming.com/ja/articles/74
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package java_lang_programming.com.android_media_demo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * ??crop
 */
public class ImageSelectionCropDemo extends AppCompatActivity {

    public final static int REQUEST_CODE_CHOOSER = 101;

    public final static int REQUEST_CODE_EXTERNAL_STORAGE = 102;

    public final static int REQUEST_CODE_CROP = 103;

    public static final List<String> types = Collections.unmodifiableList(new LinkedList<String>() {
        {
            add("image/jpeg");
            add("image/jpg");
            add("image/png");
        }
    });

    /**
     * Permissions required to read and write external storage.
     */
    private static String[] PERMISSION_EXTERNAL_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE };

    private ImageView selectedImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_selection_crop_demo);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null)
                        .show();
            }
        });

        selectedImage = (ImageView) findViewById(R.id.selected_image);
        Button btnSelectImage = (Button) findViewById(R.id.btn_select_image);
        btnSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkPermission();
            }
        });
    }

    /**
     * Called when the '???' button is clicked.
     */
    private void checkPermission() {
        for (String permission : PERMISSION_EXTERNAL_STORAGE) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                // EXTERNAL_STORAGE permission has not been granted.
                requestExternalStoragePermission();
                return;
            }
        }
        startExternalAppSelectableImage();
    }

    /**
     * Requests the READ_EXTERNAL_STORAGE permission and WRITE_EXTERNAL_STORAGE.
     * the permission is requested directly.
     */
    private void requestExternalStoragePermission() {
        // Contact permissions have not been granted yet. Request them directly.
        ActivityCompat.requestPermissions(this, PERMISSION_EXTERNAL_STORAGE,
                ImageSelectionCropDemo.REQUEST_CODE_EXTERNAL_STORAGE);
    }

    /**
     * Callback received when a permissions request has been completed.
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        switch (requestCode) {
        case (ImageSelectionCropDemo.REQUEST_CODE_EXTERNAL_STORAGE):
            if (verifyPermissions(grantResults)) {
                startExternalAppSelectableImage();
            } else {
                Toast.makeText(this, getString(R.string.permissions_not_granted), Toast.LENGTH_LONG).show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
        }
    }

    /**
     * Check that all given permissions have been granted by verifying that each entry in the
     * given array is of the value {@link PackageManager#PERMISSION_GRANTED}.
     */
    public boolean verifyPermissions(int[] grantResults) {
        // At least one result must be checked.
        if (grantResults.length < 1) {
            return false;
        }

        // Verify that each required permission has been granted, otherwise return false.
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * start ExternalApp if the required READ_EXTERNAL_STORAGE permission has been granted.
     */
    private void startExternalAppSelectableImage() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

        // Filter to only show results that can be "opened", such as a
        // file (as opposed to a list of contacts or timezones)
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            intent.putExtra(Intent.EXTRA_MIME_TYPES, types.toArray());
        }
        startActivityForResult(Intent.createChooser(intent, null), ImageSelectionCropDemo.REQUEST_CODE_CHOOSER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case (ImageSelectionCropDemo.REQUEST_CODE_CHOOSER):
            if (resultCode != RESULT_OK) {
                Toast.makeText(this, getString(R.string.image_unselected_message), Toast.LENGTH_LONG).show();
                return;
            }
            startCrop(data.getData());
            break;
        case (REQUEST_CODE_CROP):
            if (resultCode != RESULT_OK) {
                Toast.makeText(this, getString(R.string.crop_image_failure_message), Toast.LENGTH_LONG).show();
                return;
            }
            selectedImage.setImageURI(data.getData());
            deleteExternalStoragePublicPicture();
            break;
        default:
            break;
        }
    }

    /**
     * start Crop
     *
     * @param uri image uri
     */
    private void startCrop(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("aspectX", 16);
        intent.putExtra("aspectY", 9);
        intent.putExtra("scaleUpIfNeeded", true);
        intent.putExtra("scale", "true");
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.name());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getExternalStorageTempStoreFilePath()));
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(intent, ImageSelectionCropDemo.REQUEST_CODE_CROP);
    }

    /**
     * ???
     *
     * @return
     */
    private File getExternalStorageTempStoreFilePath() {
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(path, "selected_temp_image.jpg");
        return file;
    }

    /**
     * Delete temporary stored file.
     */
    private void deleteExternalStoragePublicPicture() {
        // Create a path where we will place our picture in the user's
        // public pictures directory and delete the file.  If external
        // storage is not currently mounted this will fail.
        File file = getExternalStorageTempStoreFilePath();
        if (file != null) {
            // Log.d("ImageSelectionCropDemo", file.getAbsolutePath() + " is " + file.exists());
            if (!file.delete()) {
                Log.e("ImageSelectionCropDemo", "File deletion failed.");
            }
        }
    }
}