我想使用現有的drawable並添加一些其他位圖以及文本以最終將其用作共享意圖。 我的實際代碼有效,但只有少數應用程序可以打開保存的圖像。使用它作爲郵件附件或使用信使只會顯示一個黑屏或無法打開它。繪製位圖並保存/共享
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)
{
}
處理或分享時有什麼錯誤,我必須解決,使用預期的圖像?
只是看看你的代碼,我認爲問題是你將圖像保存到其他應用程序無法讀取的應用程序的私人目錄中。嘗試將getFilesDir()更改爲Environment.getExternalStorageDir() – 2014-12-04 12:08:10
我仍然獲得了黑色背景,而不是我認爲可以獲得的圖像,但主要難以將圖像視爲mailattachment已解決,這太容易了:-)請添加此評論作爲接受它的答案! – Kooki 2014-12-04 14:29:35
完成。謝謝。祝你好運。 – 2014-12-04 21:00:43