哪位大神帮下,小米系统真机调试,裁剪界面没有出来

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="30dp" />
    <ImageView
        android:id="@+id/show_image"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_take"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:text="拍照"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_old"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:text="怀旧"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_hight"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:text="高饱和度"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.photo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private TextView txt;
    private ImageView image;
    private Button btnTake;
    private Button btnOld;
    private Button btnHight;
    private File btnFilter;
    private File file;
    private String headImagePath = Environment.getExternalStorageDirectory() + "myHeadImage";
    private String imageName = "head.jpg";
    private Bitmap headBitmap = null;
    public static final  int TAKE_PHOTO = 1;
    public static final  int CROP_PHOTO = 2;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.txt);
        image = (ImageView) findViewById(R.id.show_image);
        btnTake = (Button) findViewById(R.id.btn_take);
        btnOld = (Button) findViewById(R.id.btn_old);
        btnHight = (Button) findViewById(R.id.btn_hight);
        btnTake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".jpg");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                    uri = FileProvider.getUriForFile(MainActivity.this,"com.example.photo",file);
                } else {
                    uri = Uri.fromFile(file);
                }
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
        btnOld.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageBitmap(SelectFilter.changeTo(headBitmap,1));
            }
        });

        btnHight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageBitmap(SelectFilter.changeTo(headBitmap,2));
            }
        });
    }


    private void crop(Uri uri, int width, int height){
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ){
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop","true");
        intent.putExtra("aspectX",1);
        intent.putExtra("aspectY",1);
        intent.putExtra("outputX",width);
        intent.putExtra("outputY",height);
        //intent.putExtra("return-data",true);

        startActivityForResult(intent , CROP_PHOTO);
    }

    protected void setPicToSD(Bitmap mBitmap){
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)){
            return;
        }
        try{
            File dir = new File(headImagePath);
            dir.mkdirs();
            File file = new File(headImagePath,imageName);
            if (!file.exists()){
                file.createNewFile();
            }
                FileOutputStream output = new FileOutputStream(file);
                mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
                output.flush();
                output.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    protected void setImageToView(Intent data){
        Bundle extras = data.getExtras();
        if (extras !=null){
            headBitmap = extras.getParcelable("data");
            setPicToSD(headBitmap);
            image.setImageBitmap(headBitmap);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode ==RESULT_OK){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                        crop(uri,200,200);
                    }else {
                        crop(Uri.fromFile(file),200,200);
                    }
                }
                break;
            case CROP_PHOTO:
                if (resultCode == RESULT_OK){
                    setImageToView(data);
                    txt.setText("图片的保存路径为:"+headImagePath+imageName);
                }
                break;
            default:
                break;
        }
    }

}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.photo">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:authorities="com.example.photo"
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>
    </application>

</manifest>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题