通知和多媒体

通知的基本用法
通过NotificationManager 来管理通知
 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
                builder.setContentTitle("This is a Title");
                //builder.setStyle(new NotificationCompat.BigTextStyle().bigText("fdsfsfsdfsfsafsafsafs" +
                  //      "sdfsdfsdfsdfsfsfafsfsafsdfsdfsfsfsfsfsffwfwfwfsdfsdfsafsafasf"));
                //设置长文字
                builder.setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)));
                builder.setContentText("THis is a text");
                builder.setSmallIcon(R.mipmap.ic_launcher);
                //builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                builder.setPriority(NotificationCompat.PRIORITY_MAX);//设置通知的重要程度
                builder.setContentIntent(pi);
                builder.setAutoCancel(true);
                //builder.setSound(Uri.fromFile(new File("")));
                builder.setVibrate(new long[]{0,1000,1000,1000});//第一个和第3个参数代表静止时长,另外俩个代表震动时间
                builder.setLights(Color.GREEN,1000,1000);
                //builder.setDefaults(NotificationCompat.DEFAULT_ALL);//设置默认
                Notification notification = builder.build();
                manager.notify(1,notification);


调用照相机拍照
private void cameraPhoto() {
        File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
        try{
            if(outputImage.exists()){
                outputImage.delete();
            }
            outputImage.createNewFile();


        }catch (Exception e){
            e.printStackTrace();
        }
        if(Build.VERSION.SDK_INT >= 24){
             imageUri = FileProvider.getUriForFile(MainActivity.this,
                     "com.example.cameraalbumtest.fileprovider",outputImage);
        }else {
            imageUri = Uri.fromFile(outputImage);
        }
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(intent,TAKE_PHOTO);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case TAKE_PHOTO:
                if(resultCode == RESULT_OK){
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        imageView.setImageBitmap(bitmap);


                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }




                }
        }
    }
<provider
            android:authorities="com.example.cameraalbumtest.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            android:name="android.support.v4.content.FileProvider">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_path"/>


        </provider>
res目录下新建xml文件夹
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name = "my_images" path = ""/>
</paths>
内存卡中读取图片


private void chooseFromAlbum() {




        if(ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }else {
            openAlbum();
        }
    }


    private void openAlbum() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent,CHOOSE_ALBUM);


    }


           case CHOOSE_ALBUM:
                if(resultCode == RESULT_OK){
                    if(Build.VERSION.SDK_INT>=19){
                        handleImageOnKitKat(data);
                    }else {
                        handleImageBeforeKitKat(data);
                    }






                }


        }
    }


    private void handleImageBeforeKitKat(Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(uri,null);
        displayImage(imagePath);
   
    }


    @TargetApi(19)
    private void handleImageOnKitKat(Intent data) {
        String imagePath = null;
        Uri uri = data.getData();
        if(DocumentsContract.isDocumentUri(this,uri)){
            //如果是document类型的uri,则通过documentId处理
            String docId = DocumentsContract.getDocumentId(uri);
            if("com.android.providers.media.documents".equals(uri.getAuthority())){
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
            }else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
                Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
                imagePath = getImagePath(contentUri,null);
                
            }
            
        }else if("content".equalsIgnoreCase(uri.getScheme())){
            //如果是content了类型的Uri,则使用普通方法处理
            imagePath = getImagePath(uri,null);
        }else if("file".equalsIgnoreCase(uri.getScheme())){
            //如果是file 类型的uri 直接获取图片路径即可
            imagePath = uri.getPath();
        }
        displayImage(imagePath);


    }


    private void displayImage(String imagePath) {
        if(imagePath!=null){
            imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
        }
        
    }


    private String getImagePath(Uri externalContentUri, String selection) {
         String path = null;
        Cursor cursor = getContentResolver().query(externalContentUri,null,selection,null,null);
        if(cursor!=null){
            while (cursor.moveToNext()){
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值