调用系统文件管理器选择图片,调用系统裁剪AIP对图片处理,显示裁剪之后的图片

package com.pingyijinren.test;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity{
    private Button button;
    private ImageView imageView;
    private Uri uri;
    private File file;

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

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

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                file=null;
                try{
                    file=new File(Environment.getExternalStorageDirectory()+"/test.jpg");

                    if(file.exists()){
                        file.delete();
                    }
                    file.createNewFile();

                    uri= Uri.fromFile(file);
                    Intent intent=new Intent("android.intent.action.GET_CONTENT");
                    intent.setType("image/*");
                    startActivityForResult(intent,1);
                }
                catch(Exception e){}
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        if(requestCode==1){
            uri=data.getData();
            Intent cropIntent=new Intent("com.android.camera.action.CROP");
            cropIntent.setDataAndType(uri,"image/*");
            cropIntent.putExtra("scale",true);
            cropIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
            startActivityForResult(cropIntent,2);
        }
        else if(requestCode==2){
            try{
                Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                imageView.setImageBitmap(bitmap);
            }
            catch(Exception e){}

        }
    }
}

 

posted @ 2016-05-18 10:26  zqxLonely  阅读(271)  评论(0编辑  收藏  举报