2012-11-20 28 views
8

我想通过我的Moto Razr上的Gmail客户端共享我的内部存储文件,但每次发送到我的测试Gmail帐户时,我都会收到除附件外的所有内容。如何与Gmail客户端共享内部存储文件

这是我如何调用并启动gmail,同时添加文件作为附件。

private void saveDaily() { 
Intent intent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 
intent.setType("text/plain"); 

intent.putExtra(Intent.EXTRA_EMAIL, new String[] { loadEmailAddress() }); 
intent.putExtra(Intent.EXTRA_SUBJECT, "Daily"); 
intent.putExtra(Intent.EXTRA_TEXT, "Daily Log"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

ArrayList<Uri> uris = new ArrayList<Uri>(); 
uris.add(saveDaily2File("dailyRecord.txt")); 
Log.d(TAG_D, "Size: " + uris.size()); 
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
startActivity(Intent.createChooser(intent, "Send email")); 
} 

这就是我实现自定义内容提供者的方式。

public class SavedFileProvider extends ContentProvider { 

private static final String TAG_D = "ContentProvider"; 
private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>(); 

static { 
MIME_TYPES.put(".txt", "text/plain"); 
} 

@Override 
public String getType(Uri uri) { 
String path = uri.toString(); 

for (String extension : MIME_TYPES.keySet()) { 
    if (path.endsWith(extension)) { 
    return (MIME_TYPES.get(extension)); 
    } 
} 

return (null); 
} 

@Override 
public ParcelFileDescriptor openFile(Uri uri, String mode) 
    throws FileNotFoundException { 
Log.d(TAG_D, "openFile()"); 

File f = new File(getContext().getFilesDir(), uri.getPath()); 

Log.d(TAG_D, f.getAbsolutePath()); 
if (f.exists()) { 
    return (ParcelFileDescriptor.open(f, 
     ParcelFileDescriptor.MODE_READ_ONLY)); 
} 

throw new FileNotFoundException(uri.getPath()); 
} 

@Override 
public Cursor query(Uri url, String[] projection, String selection, 
    String[] selectionArgs, String sort) { 
throw new RuntimeException("Operation not supported"); 
} 

@Override 
public Uri insert(Uri uri, ContentValues initialValues) { 
throw new RuntimeException("Operation not supported"); 
} 

@Override 
public int update(Uri uri, ContentValues values, String where, 
    String[] whereArgs) { 
throw new RuntimeException("Operation not supported"); 
} 

@Override 
public int delete(Uri uri, String where, String[] whereArgs) { 
throw new RuntimeException("Operation not supported"); 
} 

private void copy(InputStream in, File dst) throws IOException { 
FileOutputStream out = new FileOutputStream(dst); 
byte[] buf = new byte[1024]; 
int len; 

while ((len = in.read(buf)) > 0) { 
    out.write(buf, 0, len); 
} 

in.close(); 
out.close(); 
} 

@Override 
public boolean onCreate() { 
Log.d(TAG_D, "onCreate()"); 
File f = new File(getContext().getFilesDir(), "dailyRecord.txt"); 

if (!f.exists()) { 
    AssetManager assets = getContext().getResources().getAssets(); 

    try { 
    copy(assets.open("dailyRecord.txt"), f); 
    } catch (IOException e) { 
    Log.e("FileProvider", "Exception copying from assets", e); 

    return (false); 
    } 
} 
return (true); 
} 

}

然后,我添加以下代码行中我AndroidManifest.xml文件。

<provider 
     android:name=".SavedFileProvider" 
     android:authorities="Package Path here" 
     android:exported="true" 
     android:grantUriPermissions="true" 
     android:multiprocess="true" > 
</provider> 

我想知道我在这里失踪。

我检查链接:Link1Link2

+0

你可以检查你的方法getType()和查询(Uri uri,String []投影,字符串arg2,字符串[] arg3,字符串arg4)是否被调用?如果是这样,我可以为您提供一些代码,以便可以同时使用Gmail和默认电子邮件客户端。 – Snicolas

+0

我会检查。谢谢。 – antonio081014

+0

看到我的答案在这里:http://stackoverflow.com/questions/15377373/how-to-put-a-video-file-in-android-custom-content-provider – dangalg

回答

3

我用这个:

AndroidManifest.xml中

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

按钮点击:

 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)); 
       } 
     } 

我的内容提供商类

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

这也适用于动态生成的文件或只为捆绑资产? – Alex

相关问题