How to share internal storage file with Gmail Client

前端 未结 1 585
清歌不尽
清歌不尽 2021-01-17 20:10

I am trying to share my internal storage file via Gmail client on my Moto Razr, but every time I sent to my test gmail account, I got everything except attachment.

T

相关标签:
1条回答
  • 2021-01-17 21:11

    I use this:

    AndroidManifest.xml

    <provider android:name="com.myapp.main.MyContentProvider" android:authorities="com.myapp.main"></provider>
    

    Button click:

            public void onClick(View v) {
    
                List<Intent> targetedShareIntents = new ArrayList<Intent>();
                 Intent shareIntent = new Intent(Intent.ACTION_SEND);
                 shareIntent.setType("image/jpg");
                 Uri theUri = Uri.parse("content://com.myapp.main/"+srcImage);
                 List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
                 int i=0;
                 List<ResolveInfo> reInfoToDelete = new ArrayList<ResolveInfo>();
                 if (!resInfo.isEmpty()){
                        for (ResolveInfo resolveInfo : resInfo) {
                            String packageName = resolveInfo.activityInfo.packageName;
                            Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
                            targetedShareIntent.setType("image/jpg");
                            targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share file");
                            if (packageName.equals("com.google.android.gm")){
                                 targetedShareIntent.setType("image/png");
    
                                 targetedShareIntent.putExtra(Intent.EXTRA_TEXT, "some text");
                                 targetedShareIntent.putExtra(Intent.EXTRA_STREAM, theUri);
                                 targetedShareIntent.setPackage(packageName);
                                 targetedShareIntents.add(targetedShareIntent);
    
                            }
                        }
    
    
    
                        startActivity(targetedShareIntents.remove(0));
                   }
            }
    

    My Content Provider Class

        public class MyContentProvider extends ContentProvider implements PipeDataWriter<InputStream>{
    
        @Override
        public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
            //Adapt this to your code
            AssetManager am = getContext().getAssets();
            String file_name = "path/"+uri.getLastPathSegment();
            if(file_name == null)
                throw new FileNotFoundException();
            AssetFileDescriptor afd = null;
            try {
                afd = am.openFd(file_name);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return afd;//super.openAssetFile(uri, mode);
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public String getType(Uri uri) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public boolean onCreate() {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection,
                String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        public void writeDataToPipe(ParcelFileDescriptor arg0, Uri arg1,
                String arg2, Bundle arg3, InputStream arg4) {
            // Transfer data from the asset to the pipe the client is reading.
            byte[] buffer = new byte[8192];
            int n;
            FileOutputStream fout = new FileOutputStream(arg0.getFileDescriptor());
            try {
                while ((n=arg4.read(buffer)) >= 0) {
                    fout.write(buffer, 0, n);
                }
            } catch (IOException e) {
                Log.i("InstallApk", "Failed transferring", e);
            } finally {
                try {
                    arg4.close();
                } catch (IOException e) {
                }
                try {
                    fout.close();
                } catch (IOException e) {
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题