我整个上午一直在与一个问题作斗争,并且以为我现在会问这里。
我使用以下代码将9个补丁图像附加到电子邮件中:
sendIntent.setType("image/png");
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.parse("android.resource://com.android9patch.viewer/raw/" + mBaseFilename + String.format("%05d", mAppBackgroundCurrentFile)) );
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(sendIntent, "Email:"));
问题在于,当我收到电子邮件时,图像不是原始的9补丁,而是没有缩放和填充标记的版本。
我应该得到这个结果:
但我得到了这个:
我怀疑应用程序在发送之前处理原始文件?
其他信息:
我现在正尝试将文件保存到SDCARD,然后再将其附加到电子邮件中。好吧,出于某种原因,即使复制也会删除缩放和填充标记...我不明白。
这是我从raw copy function获取的复制功能。
private boolean copyToSDCard( int resourceID, String finalName )
{
String extStorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
OutputStream out = null;
try
{
InputStream in = getResources().openRawResource( resourceID );
out = new FileOutputStream(extStorageDirectory + "/" + finalName + ".9.png");
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch (Exception e)
{
Log.e("copyToSDCard", e.toString());
e.printStackTrace();
}
return false;
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
答案 0 :(得分:0)
我最终将资产复制到SD卡并使用以下功能将此新文件附加到电子邮件中:
...
if( copyAssetToSDCard( filename, basepath + filename ) )
{
uris.add( Uri.parse( "file://" + basepath + filename ) );
}
...
private boolean copyAssetToSDCard( String SrcFilename, String DstFilename )
{
OutputStream out = null;
try
{
AssetManager assetManager = getResources().getAssets();
InputStream in = assetManager.open(SrcFilename);
out = new FileOutputStream(DstFilename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
return true;
}
catch (Exception e)
{
Log.e("copyToSDCard", e.toString());
e.printStackTrace();
}
return false;
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}