0

我是android领域的新手,我尝试在设置像Whatsapp这样的个人资料照片时裁剪图像。我开发了程序但无法得到答案。如果我尝试打开我的相机按钮,它将打开并仅捕获图片它赢了`t 移动到裁剪会话。它发生在我尝试打开画廊时。在这里,我附上了我的编码:

package com.example.ds.newcrop;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

import static android.os.Build.VERSION_CODES.M;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Toolbar toolbar;
    File file;
    Uri uri;
    Intent CamIntent,GalIntent,CropIntent;
    final int RequestPermissionCode=1;
    DisplayMetrics displayMetrics;
    int width,height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Crop Image");
        setSupportActionBar(toolbar);

        imageView = (ImageView) findViewById(R.id.imageView);

        int permissioncheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
        if(permissioncheck == PackageManager.PERMISSION_DENIED)
            RequestRuntimePermission();
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());
    }

   // StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());
    private void RequestRuntimePermission() {
        if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.CAMERA))
            Toast.makeText(this,"Camera Permission allows us to access CAMERA App",Toast.LENGTH_SHORT).show();
        else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, RequestPermissionCode);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.btn_camera) {
            CameraOpen();
        }
        else if (item.getItemId() == R.id.btn_gallery){
            GalleryOpen();
        }
        return true;
    }

    private void GalleryOpen() {
        GalIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(GalIntent,"Select Image from Gallery"),2);
    }

    private void CameraOpen() {
        CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        file = new File(Environment.getExternalStorageDirectory(),
                "tmp_"+String.valueOf(System.currentTimeMillis())+".jpg");
        /*Log.e("filecheck",Environment.getExternalStorageDirectory()+
                "file"+String.valueOf(System.currentTimeMillis())+"jpg");
        if(file == null)
            Log.e("filecheck","filenull");*/
       Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);
       // uri = Uri.fromFile(file);
        CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
        CamIntent.putExtra("return-data",true);
        startActivityForResult(CamIntent,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //Log.e("filecheck","filerc"+requestCode+"/"+resultCode);
        if(requestCode == 0 && resultCode == RESULT_OK )
            CropImage();
        else if(requestCode == 2 && resultCode == RESULT_OK) {
            if(data!=null)
            {
                uri = data.getData();
                CropImage();
            }
        }
        else if (requestCode == 1 && resultCode == RESULT_OK)
        {
            if(data!=null)
            {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = bundle.getParcelable("data");
                imageView.setImageBitmap(bitmap);
            }
        }
    }

    private void CropImage() {
        final int width  = 400;
        final int height = 200;
        try {
            CropIntent = new Intent("com.android.camera.action.CROP");
             Uri contentUri;

            if(Build.VERSION.SDK_INT > M){
                if(file == null)
                    Log.e("filecheck","filenull");
                Log.e("filecheck",file.getAbsolutePath());

                contentUri = FileProvider.getUriForFile(MainActivity.this,
                        "com.example.ds.newcrop.provider",
                        file);//package.provider

                //TODO:  Permission..

                getApplicationContext().grantUriPermission("com.android.camera",
                        contentUri,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

                CropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                CropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            }else{

                contentUri = Uri.fromFile(file);

            }

            CropIntent.setDataAndType(uri,"image/*");

            CropIntent.putExtra("crop","true");
            CropIntent.putExtra("outputX",180);
            CropIntent.putExtra("outputY",180);
            CropIntent.putExtra("aspectX",3);
            CropIntent.putExtra("aspectY",4);
            CropIntent.putExtra("ScaleUpIfNeeded",true);
            CropIntent.putExtra("return-data",true);
            CropIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);

            startActivityForResult(CropIntent,1);
        }
        catch(ActivityNotFoundException ex)
        {
             Log.e("Activity Not Found",""+ex.toString());
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch(requestCode)
        {
            case RequestPermissionCode:
            {
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(this,"Permission Denied",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

我还附加了所有清单和路径提供程序。但它不起作用。请任何人给我一个解决方案。提前致谢。

4

1 回答 1

0

使用“com.android.camera.action.CROP”非常危险,因为并非所有设备都存在 AOSP 相机应用程序。你可以在这里阅读更多关于它的信息。我建议使用第三方图像裁剪库。我个人更喜欢这个,因为它有很多自定义选项。

于 2018-05-24T07:57:25.207 回答