Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nvoids.pradyut.nvoidsapp;
- import android.Manifest;
- import android.app.Activity;
- import android.content.ActivityNotFoundException;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.media.MediaScannerConnection;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.FileProvider;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class ProfileImage extends AppCompatActivity {
- ImageView imageView;
- Button buttonCamera, buttonGallery ;
- File file;
- private static Uri imageUri;
- Long filetime;
- private Context ct;
- Object obj;
- Intent CamIntent, GalIntent, CropIntent ;
- public static final int RequestPermissionCode = 1 ;
- DisplayMetrics displayMetrics ;
- int width, height;
- private static String[] PERMISSIONS_CAMERA = {
- Manifest.permission.CAMERA
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_profileimage);
- imageView = (ImageView)findViewById(R.id.imageview);
- buttonCamera = (Button)findViewById(R.id.button2);
- buttonGallery = (Button)findViewById(R.id.button1);
- EnableRuntimePermission();
- verifyCameraPermissions(this);
- ct = this;
- obj = this;
- buttonCamera.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- ClickImageFromCamera() ;
- }
- });
- buttonGallery.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- GetImageFromGallery();
- }
- });
- }
- public static void verifyCameraPermissions(Activity activity) {
- // Check if we have write permission
- int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
- if (permission != PackageManager.PERMISSION_GRANTED) {
- // We don't have permission so prompt the user
- ActivityCompat.requestPermissions(activity, PERMISSIONS_CAMERA, 1);
- }
- }
- public void ClickImageFromCamera() {
- try {
- filetime = System.currentTimeMillis();
- CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids",
- "file" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
- imageUri = FileProvider.getUriForFile(this, "com.nvoids.provider",
- new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids",
- "file " + filetime + ".jpeg"));
- //uri = Uri.fromFile(file);
- CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
- CamIntent.putExtra("return-data", true);
- startActivityForResult(CamIntent, 0);
- } catch (Exception e) {
- Log.d("ListView1", "Capture from Camera exception: " + e.toString());
- }
- }
- public void GetImageFromGallery(){
- GalIntent = new Intent(Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(Intent.createChooser(GalIntent, "Select Image From Gallery"), 2);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- //Log.d("ListView1", "requestCode: " + requestCode);
- if (requestCode == 0 && resultCode == RESULT_OK) {
- try {
- //sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, imageUri));
- } catch ( Exception e) {
- Log.d("ListView1", "Refresh gallery exception: " + e.toString());
- }
- String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids/file " + filetime +".jpeg";
- try {
- MediaScannerConnection.scanFile(this,
- new String[]{ filename }, null,
- new MediaScannerConnection.OnScanCompletedListener() {
- public void onScanCompleted(String path, Uri uri) {
- Log.d("ListView1", "ExternalStorageScanned " + path);
- //Log.d("ListView1", "ExternalStorage -> uri=" + uri);
- }
- });
- } catch (Exception e) { Log.d("ListView1", "MediaScanner exception: " + e.toString()); }
- imageUri=Uri.parse(filename);
- Log.d("ListView1", "ImageUri: " + imageUri.toString());
- ImageCropFunction();
- } else if (requestCode == 2) {
- if (data != null) {
- try {
- //Log.d("ListView1", "ImageUri: " + imageUri.toString());
- } catch ( Exception e) { Log.d("ListView1", "ImageUri exception: " + e.toString()); }
- imageUri = data.getData();
- ImageCropFunction();
- } else {
- Log.d("ListView1", "Data is null");
- }
- } else if (requestCode == 1) {
- Log.d("ListView1", "Inside 1");
- if (data != null) {
- Log.d("ListView1", "Data is not null");
- Bitmap bitmap = null;
- try {
- Bundle bundle = data.getExtras();
- bitmap = bundle.getParcelable("data");
- imageView.setImageBitmap(bitmap);
- } catch (Exception e) {
- Log.d("ListView1", "Image view set exception: "+ e.toString());
- }
- try {
- File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids/toServer.jpeg");
- file.createNewFile();
- FileOutputStream fos = new FileOutputStream(file);
- final BufferedOutputStream bos = new BufferedOutputStream(fos, 1024 * 8);
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
- bos.flush();
- bos.close();
- fos.close();
- } catch (Exception e) {
- Log.d("ListView1", "Save to file exception: "+ e.toString());
- }
- } else {
- Log.d("ListView1", "Data is null");
- }
- }
- }
- public void ImageCropFunction() {
- // Image Crop Code
- try {
- try {
- Log.d("ListView1", "ImageUri: " + imageUri.toString()); } catch ( Exception e) { Log.d("ListView1", "ImageUri exception: " + e.toString()); }
- CropIntent = new Intent("com.android.camera.action.CROP");
- CropIntent.setDataAndType(imageUri, "image/*");
- CropIntent.putExtra("crop", "true");
- CropIntent.putExtra("outputX", 50000);
- CropIntent.putExtra("outputY", 50000);
- CropIntent.putExtra("aspectX", 1);
- CropIntent.putExtra("aspectY", 1);
- CropIntent.putExtra("scaleUpIfNeeded", true);
- CropIntent.putExtra("return-data", true);
- startActivityForResult(CropIntent, 1);
- } catch (ActivityNotFoundException e) {
- Log.d("ListView1", "ImageCropFunction Activity Not Found exception: " + e.toString());
- } catch (Exception e) {
- Log.d("ListView1", "ImageCropFunction exception: " + e.toString());
- }
- }
- //Image Crop Code End Here
- public void EnableRuntimePermission(){
- if (ActivityCompat.shouldShowRequestPermissionRationale(ProfileImage.this,
- Manifest.permission.CAMERA))
- {
- Toast.makeText(ProfileImage.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
- } else {
- ActivityCompat.requestPermissions(ProfileImage.this,new String[]{
- Manifest.permission.CAMERA}, RequestPermissionCode);
- }
- }
- @Override
- public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
- switch (RC) {
- case RequestPermissionCode:
- if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
- //Toast.makeText(ProfileImage.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(ProfileImage.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement