绘制位图并保存/共享它

时间:2014-12-04 11:44:14

标签: android android-intent bitmap android-canvas

我想使用现有的drawable并添加一些其他Bitmaps以及文本,最后将其用作共享意图。 我的实际代码有效,但只有少数应用可以打开保存的图像。使用它作为邮件附件或使用信使只显示黑屏或无论如何都无法打开它。

            Bitmap bitmap =  ImageLoader.getInstance().loadImageSync("drawable://" + R.drawable.my_image);
            android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
            if(bitmapConfig == null) 
            {
              bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
            }
            bitmap = bitmap.copy(bitmapConfig, true);   
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.rgb(61, 61, 61));
            paint.setTextSize((int) (10));
            paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

            canvas.drawColor(0, Mode.CLEAR);
            canvas.drawBitmap(ImageLoader.getInstance().loadImageSync("drawable://" + R.drawable.image1), canvas.getWidth() - 100, 10, paint);
            canvas.drawBitmap(ImageLoader.getInstance().loadImageSync("drawable://" + R.drawable.image2), canvas.getWidth() - 50, 10, paint);

            canvas.drawText("TEST", 100, 10, paint);            
            canvas.drawText("TEST1", 10, canvas.getHeight() - 10, paint);   
            canvas.drawText("TEST2", 10 + (canvas.getWidth() / 2), canvas.getHeight() - 10, paint);
            File file = new File(getFilesDir(), "TESTIMAGE_" + counter + ".png");
            FileOutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();

            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
            shareIntent.setType("image/png");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(shareIntent, "Share image"));
        } 
        catch(Exception ex) 
        {


        }

处理或分享时是否有任何错误,我必须解决,按预期使用图像?

1 个答案:

答案 0 :(得分:1)

其他应用无法读取附件,因为您要将其保存到getFilesDir()

只需将getFilesDir()更改为Environment.getExternalStorageDir(),其他应用就可以处理已保存的图片。这还要求您在AndroidManifest中添加以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我不知道为什么图像是黑色的,我必须看到资源。祝你好运。

相关问题