仿抖音短视频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;

    }
}
目录
相关文章
|
7天前
|
开发工具
uniapp, 短剧视频类App实现参考,支持滑动播放,仿抖音 仿陌陌 短视频 无限滑动播放 视频流
阿里云点播服务web播放器sdk,短剧视频类App实现参考。仿抖音 仿陌陌 短视频 无限滑动播放 视频流。无uniapp video 原生组件的层级、遮挡、覆盖问题,适合与不同功能视图组合使用,实现丰富的应用功能。
uniapp, 短剧视频类App实现参考,支持滑动播放,仿抖音 仿陌陌 短视频 无限滑动播放 视频流
|
1月前
仿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+部署文档+讲解等)
|
6天前
|
移动开发 Android开发 数据安全/隐私保护
移动应用与系统的技术演进:从开发到操作系统的全景解析随着智能手机和平板电脑的普及,移动应用(App)已成为人们日常生活中不可或缺的一部分。无论是社交、娱乐、购物还是办公,移动应用都扮演着重要的角色。而支撑这些应用运行的,正是功能强大且复杂的移动操作系统。本文将深入探讨移动应用的开发过程及其背后的操作系统机制,揭示这一领域的技术演进。
本文旨在提供关于移动应用与系统技术的全面概述,涵盖移动应用的开发生命周期、主要移动操作系统的特点以及它们之间的竞争关系。我们将探讨如何高效地开发移动应用,并分析iOS和Android两大主流操作系统的技术优势与局限。同时,本文还将讨论跨平台解决方案的兴起及其对移动开发领域的影响。通过这篇技术性文章,读者将获得对移动应用开发及操作系统深层理解的钥匙。
|
10天前
|
XML 移动开发 前端开发
使用duxapp开发 React Native App 事半功倍
对于Taro的壳子,或者原生React Native,都会存在 `android` `ios`这两个文件夹,而在duxapp中,这些文件夹的内容是自动生成的,那么对于需要在这些文件夹中修改的配置内容,例如包名、版本号、新架构开关等,都通过配置文件的方式配置了,而不需要需修改具体的文件
|
10天前
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
下一篇
无影云桌面