Android分享功能

本文深入探讨了Android中处理分享数据的过程,包括从不同来源接收数据(如ES浏览器、三星自带文件管理器、系统相册)并进行相应处理的方法。通过解析Intent的使用和数据类型,展示了如何在Android应用中高效地管理和操作分享的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://developer.android.com/training/sharing/send.html

http://developer.android.com/training/sharing/receive.html


ES浏览器过来的数据

[file:///sdcard/Vlog.xml, file:///sdcard/toolbox-stericson, file:///sdcard/videoEngine.log, file:///sdcard/python.zip]


三星自带文件管理器过来数据

file:///mnt/sdcard/ubuntu-11.10-desktop-amd64.iso


系统相册过来的数据

[content://media/external/images/media/748, content://media/external/images/media/744, content://media/external/images/media/743, content://media/external/images/media/742]


private void getShareData()
    {
        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action) && type != null)
        {
           handleSingleData(intent);
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null)
        {
            handleMultipleData(intent);
        } else
        {
            LogUtil.d("Handle other intents");
            // Handle other intents, such as being started from the home screen
        }
    }

    private void handleMultipleData(Intent intent)
    {
        ArrayList<Parcelable> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        for(Parcelable pa:imageUris){
            Uri uri = (Uri)pa;
            String scheme = uri.getScheme();
            if(scheme.equals("content")){
                ContentResolver cr = getContentResolver();
                Cursor c = cr.query(uri,null,null,null,null);
                c.moveToFirst();
                String filePath = c.getString(c.getColumnIndexOrThrow(Images.Media.DATA));
                c.close();
                LogUtil.d("filePath = " + filePath);
            }else if(scheme.equals("file"))
            {
                String filePath = uri.getPath();
                LogUtil.d("filePath = " + filePath);
            }
        }
    }

    private void handleSingleData(Intent intent)
    {
        Uri uri = (Uri)intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
        String scheme = uri.getScheme();
        if(scheme.equals("content")){
            ContentResolver cr = getContentResolver();
            Cursor c = cr.query(uri,null,null,null,null);
            c.moveToFirst();
            String filePath = c.getString(c.getColumnIndexOrThrow(Images.Media.DATA));
            c.close();
            LogUtil.d("filePath = " + filePath);
        }else if(scheme.equals("file"))
        {
            String filePath = uri.getPath();
            LogUtil.d("filePath = " + filePath);
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值