weixin_43698148 2019-05-19 23:03 采纳率: 0%
浏览 206

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

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog