第一行代码-多媒体通知 相机 相册 音频视频播放

  1. 通知

    send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Notification notification = null;
                    Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                    PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    //适配Android8.0 版本
                    /**
                     * Oreo不用Priority了,用importance
                     * IMPORTANCE_NONE 关闭通知
                     * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
                     * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
                     * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
                     * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
                     */
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        NotificationChannel mchannel = new NotificationChannel("mychannel01", "scy",NotificationManager.IMPORTANCE_HIGH);
                        //通知小圆
                        mchannel.setShowBadge(true);
                        //可以震动
                        mchannel.enableVibration(true);
                        //闪烁灯指示
                        mchannel.enableLights(true);
                        manager.createNotificationChannel(mchannel);
                        notification = new Notification.Builder(MainActivity.this, "mychannel01")
                                .setContentTitle("我在8.0版本 ")
                                .setContentText("this is content text this is content text this is content text vthis is content text this is content text this is content text ")
                                .setWhen(System.currentTimeMillis())
                                .setSmallIcon(R.mipmap.ic_launcher_round)
                                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                                .setContentIntent(pi)
                                .setAutoCancel(true)//点击消息后会取消。
                                .setNumber(1)
                                .setStyle(new Notification.BigTextStyle().bigText("this is content text this is content text this is content text vthis is content text this is content text this is content text "))
                                .build();
                    } else {
                        notification = new Notification.Builder(MainActivity.this)
                                .setPriority(NotificationCompat.PRIORITY_MAX)
                                .setContentTitle("this is content title ")
                                .setContentText("this is content text ")
                                .setWhen(System.currentTimeMillis())
                                .setSmallIcon(R.mipmap.ic_launcher_round)
                                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                                .setContentIntent(pi)
                                //.setSound(Uri.fromFile(new File("")))
                                //.setVibrate(new long[] {0,1000,0,0})
                                //.setLights(Color.GREEN,1000,1000)
                                .setDefaults(NotificationCompat.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .build();
                    }
                    manager.notify(1,notification);
                }
            });
  2. 调用摄像头和图库并显示

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File image = new File(getExternalCacheDir(),"output_image.jpg");
            try {
                if (image.exists()) {
                    image.delete();
                }
                image.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //从Android7.0 开始就认为本地的直接路径Uri不安全
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                imageUri = FileProvider.getUriForFile(MainActivity.this,
                        "com.scy.android.cameraalbmtest.fileprovider", image);
            } else {
                imageUri = Uri.fromFile(image);
            }
    
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_PHOTO);
        }
    });
    
    case TAKE_PHOTO:
        if (resultCode == RESULT_OK) {
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                mImageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        break;
    
    <provider
        android:authorities="com.scy.android.cameraalbmtest.fileprovider"
        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>
    
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="my_images" path="" />
    </paths>
    cButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
            } else {
                openAlbum();
            }
        }
    });
    
    private void openAlbum() {
       intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    openAlbum();
                } else {
                    Toast.makeText(this, "you deny the pemission", Toast.LENGTH_SHORT).show();
                }
                break;
                default:
        }
    }
    
    case CHOOSE_PHOTO:
        if (requestCode == RESULT_OK) {
          if (data != null) {
        mImageView.setImageURI(data.getData());
    }
        break;
    default:
        break;

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值