仿抖音短视频APP源码,实现简单的换头像并保存

简介: 仿抖音短视频APP源码,实现简单的换头像并保存
public class My_info extends AppCompatActivity implements View.OnClickListener {
private static final int CHOOSE_PHOTO=2;
private CircleImageView image;//一种用于图片圆形化的控件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
    image=(CircleImageView)findViewById(R.id.p_pic);
       if(readPac()!=null){        
              image.setImageBitmap(readPac());//如果头像修改过了,内存中已经新的头像图片。
        }
    @Override
    public void onClick(View v){
        switch (v.getId()){
             case R.id.alter:        //点击修改头像事件
             /
                if(ContextCompat.checkSelfPermission(My_info.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.
                PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(My_info.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                    //以上是访问图库的运行时权限/
                }else
                {
                    open();//打开图库
                }
                break;
            default: break;
        }
    }
    @Override
     //这里是对用户是否同意访问图库的处理
    public void onRequestPermissionsResult(int requestCode,String[] permissions,int [] grantResults){              
        switch (requestCode){
            case 1:
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    open();
                }else{
                    Toast.makeText(My_info.this,"您拒绝了权限",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
    //打开图库
    private void open(){
        Intent intent=new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent,CHOOSE_PHOTO);
    }

//
    @Override
    protected void onActivityResult(int requestCode,int reultCode,Intent data){
        switch (requestCode){
            case CHOOSE_PHOTO:
                if(reultCode==RESULT_OK){
                    handle(data);
                }
                break;
        }
    }
    private void handle(Intent data){
        String imagePath=null;
        Uri uri=data.getData();
        if(DocumentsContract.isDocumentUri(this,uri)){
            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.dowmloads.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())){
                imagePath=getImagePath(uri,null);
            }else if("file".equalsIgnoreCase(uri.getScheme())){
                imagePath=uri.getPath();
           }
            displayImage(imagePath);

        }

    }
    private String getImagePath(Uri uri,String selection){
        String path=null;
        Cursor cursor=getContentResolver().query(uri,null,selection,null,null);
        if(cursor!=null){
            if(cursor.moveToFirst()){
                path=cursor.getString(cursor.getColumnIndex(MediaStore.Ima//ges.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }

    private void displayImage(Str/ing imagePath){
        if(imagePath!=null){
            Bitmap bitmap= BitmapFactory.decodeFile(imagePath);
            image.setImageBitmap(bitmap);
            Toast.makeText(My_info.this,"获取图片成功",Toast.LENGTH_SHORT).show();
            savePac(bitmap);

        }else {
            Toast.makeText(My_info.this,"获取图片失败",Toast.LENGTH_SHORT).show();
        }
    }

//以上获取图片的绝对路径再获取bitmap实例过程来自《第一行代码 第2版》P299//
//
//
//
    private void savePac(Bitmap bitmap){//保存图片
        String TargetPath=My_info.this.getFilesDir().toString()+File.separator+"imagic";//getFilesDir()方法用于获取/data/data//files目录
      File file=new File(TargetPath,"pac.jpg");
      file.delete();
      if(!file.exists()){
          file.getParentFile().mkdirs();
      }
      try{
          FileOutputStream fileOutputStrea=new FileOutputStream(file);
          bitmap.compress(Bitmap.CompressFormat.JPEG,80,fileOutputStream);
          fileOutputStream.flush();
          fileOutputStream.close();
      }catch (Exception e){
          Log.d("cnm", e.toString());
      }
    }
    private Bitmap readPac(){//读取图片
       String  savePath=My_info.this.getFilesDir().toString()+File.separator+"imagic"+File.separator+"pac.jpg";
        Bitmap bitmap = null;
        try{
                bitmap = BitmapFactory.decodeFile(savePath);
        } catch (Exception e) {}
        return bitmap;

    }
}
目录
相关文章
|
6天前
|
开发工具
uniapp, 短剧视频类App实现参考,支持滑动播放,仿抖音 仿陌陌 短视频 无限滑动播放 视频流
阿里云点播服务web播放器sdk,短剧视频类App实现参考。仿抖音 仿陌陌 短视频 无限滑动播放 视频流。无uniapp video 原生组件的层级、遮挡、覆盖问题,适合与不同功能视图组合使用,实现丰富的应用功能。
uniapp, 短剧视频类App实现参考,支持滑动播放,仿抖音 仿陌陌 短视频 无限滑动播放 视频流
|
29天前
仿SOUL社交友附近人婚恋约仿陌陌APP网站源码
仿SOUL社交友附近人婚恋约仿陌陌APP网站源码
40 0
仿SOUL社交友附近人婚恋约仿陌陌APP网站源码
|
2月前
|
存储 BI Android开发
全开源仿第八区H5APP封装打包分发系统源码
全开源仿第八区H5APP封装打包分发系统源码
98 4
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的房屋租赁App的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的房屋租赁App的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的房屋租赁App的详细设计和实现(源码+lw+部署文档+讲解等)
|
2月前
|
C# 开发工具
【Azure 应用服务】Azure Function App使用SendGrid发送邮件遇见异常消息The operation was canceled,分析源码渐入最源端
【Azure 应用服务】Azure Function App使用SendGrid发送邮件遇见异常消息The operation was canceled,分析源码渐入最源端
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的成人教育APP的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的成人教育APP的详细设计和实现(源码+lw+部署文档+讲解等)
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的多功能智能手机阅读APP的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的多功能智能手机阅读APP的详细设计和实现(源码+lw+部署文档+讲解等)
|
程序员 开发工具 开发者
短视频APP开发者告诉你:苹果手机加上它也能用美颜
如果苹果手机加入了它,微信也能自带美颜,那么,“它”是什么呢?
|
开发者
短视频APP开发者们群雄逐鹿,谁能领先
,那么多短视频APP群雄逐鹿,为何抖音遥遥领先?看完这篇分析,或许你能有所感悟。
|
开发者 数据安全/隐私保护
解决了,短视频APP开发者最头疼的苹果上架相关问题
在短视频APP开发者们把软件开发完毕后,APP就要开始上架了,在APP上架苹果商店时,我们需要一个苹果开发者账号,本文主要为大家讲解苹果开发者账号的申请流程是什么,很多人都不会,特此写下这篇文章,希望能为一筹莫展的朋友提供一些帮助。
下一篇
无影云桌面