Java tutorial
package com.truemind.selidpic_v20.ui; import android.Manifest; import android.annotation.TargetApi; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; import android.widget.ImageView; import com.truemind.selidpic_v20.BaseActivity; import com.truemind.selidpic_v20.R; import com.truemind.selidpic_v20.util.CommonDialog; import java.io.FileNotFoundException; import java.io.InputStream; import uk.co.senab.photoview.PhotoViewAttacher; /** * Created by ? on 2017-04-24. * * @notice After android 6.0 (Marshmallow), Permission issue occurs * Read/Write from storage, or Camera or else need permission during RUNTIME * Users have to grant their permission due to accessibility, * and provider (application) must check it's permission matter, periodically. * * Using PhotoView, Apache licensed on ChrisBanes * */ public class GalleryActivity extends BaseActivity { private static final String TAG = "MyTag"; private String selectedImagePath; private static final int SELECT_PICTURE = 1; private static final int MY_PERMISSION_REQUEST_STORAGE = 1; private ImageView photoView; private Bitmap image_bitmap; private InputStream imageStream; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallery); initView(); initFooter(); initFloating(); floatingListener(getContext()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { checkPermission(); } else { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); } } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); //resized = resizeBitmap(selectedImagePath); Bitmap resized = resizeBitmapWithURL(selectedImageUri); photoView.setImageBitmap(resized); PhotoViewAttacher mAttacher = new PhotoViewAttacher(photoView); mAttacher.update(); } } } /** * ? ? ?? Uri * ?? path * * @param uri ? ? ? ?? Uri * @return String string ?? path * */ @SuppressWarnings("deprecation") public String getPath(Uri uri) { if (uri == null) { return null; } String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } return uri.getPath(); } /**Android 6.0 ?? ? . * * @param imagePath Uri ? ? path * @return Bitmap 1/4 ? ImageView * */ public Bitmap resizeBitmap(String imagePath) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; image_bitmap = BitmapFactory.decodeFile(imagePath, options); int dstWidth = image_bitmap.getWidth(); int dstHeight = image_bitmap.getHeight(); return Bitmap.createScaledBitmap(image_bitmap, dstWidth, dstHeight, true); } /**Android 6.0 ?? ??. * ? * * @param ImageUri ? ImageSelect ?? Uri * @return Bitmap 1/4 ? ImageView * */ public Bitmap resizeBitmapWithURL(Uri ImageUri) { try { imageStream = getContentResolver().openInputStream(ImageUri); } catch (FileNotFoundException e) { e.printStackTrace(); } image_bitmap = BitmapFactory.decodeStream(imageStream); int dstWidth = image_bitmap.getWidth() / 2; int dstHeight = image_bitmap.getHeight() / 2; return Bitmap.createScaledBitmap(image_bitmap, dstWidth, dstHeight, true); } public void initView() { photoView = (ImageView) findViewById(R.id.photoView); } /** * Android 6.0??? ?? ? * Permission ? ? ?? ? * */ @TargetApi(Build.VERSION_CODES.M) private void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { /** Access denied (Permission denied)*/ if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { //?? - ? ?? ? ? ? ? . //??? ' ' ? ?? ?. Log.d(TAG, "?? ."); ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MY_PERMISSION_REQUEST_STORAGE); } else { Log.d(TAG, " ? ."); ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MY_PERMISSION_REQUEST_STORAGE); } } else { /** Access already granted (Permission granted)*/ Log.d(TAG, "Permission is granted"); Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case MY_PERMISSION_REQUEST_STORAGE: // if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { /** Access granted (Permission granted)*/ Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); } else { /** Access denied (user denied permission)*/ CommonDialog dialog = new CommonDialog(); dialog.setOnCloseListener(new CommonDialog.OnCloseListener() { @Override public void onClose(DialogInterface dialog, int which, Object data) { if (which == 1) { finish(); } } }); dialog.showDialog(this, "? .", false); } } } @Override public void onBackPressed() { super.onBackPressed(); finish(); } }