Search code examples
androidnine-patch

9 patch png raw resources attached to email are not original files


I have been fighting with a problem all morning and thought I would ask here now.

I use the following code to attach 9 patch images to an email:

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:"));

The problem is that when I get the email, the image is not the original 9 patch, but the version without the scale and padding markers.

I should get this result: Expected result attachment

But I get this instead: Result received

I suspect that the app processes the raw file before sending it?

Additional information:

I am now attempting to save the file to the SDCARD before attaching them to the email. Well, for some reason, even copying removes the scale and padding markers... I don't get it.

Here is my copy function I took from 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);
    }
}

Solution

  • I ended up copying the asset to the sdcard and attaching this new file to the email using the following functions:

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