在电子邮件中附加内部存储编码位图字符串

时间:2017-03-10 23:27:42

标签: android email bitmap sharedpreferences

我正在内部存储中编写和读取图像作为字符串。两种方法如下:

public static void writeImageInternalStorage( Bitmap bitmap, Context context ){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences( context );
        SharedPreferences.Editor edit       = sharedPreferences.edit();
        edit.putString( "sequence_password_boxes",encodedImage );
        edit.apply();
    }

    public static Bitmap readImageInternalStorage( Context context ){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences( context );
        String storedImage = sharedPreferences.getString( "sequence_password_boxes", "");
        if( !storedImage.equalsIgnoreCase( "" ) ){
            byte[] b = Base64.decode(storedImage, Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        }
        return null;
    }

工作正常。我需要将内部存储的读取图像附加到电子邮件中。当图像来自外部存储时,我很成功:

File file = new File( Environment.getExternalStorageDirectory(), context.getString( R.string.screenshot_name ) );
    if( file.exists() || file.canRead() ){
        Uri uri = Uri.fromFile( file );
        sendIntent.putExtra( Intent.EXTRA_STREAM, uri );
    }

在我的新案例中,我无法将文件路径传递给Uri。我相信我需要解码图像以正确附加它。 我是否需要获取内部存储文件路径并在解码图像之后?这种情况是不可能的,因为Uri需要一个流。 我该怎么办?

0 个答案:

没有答案