编辑 - 另一个Udpate!
有趣的是我在nexus 5上尝试了相同的代码,图像预览是正确的,但图像本身只是黑色。
正在使用的设备为s3 minis,这些设备无法打开,也无法显示预览。
我的目标是sdk 16版。
除此之外,我将我的nexus插回到我的开发机器中并作为存储设备打开,图像未显示。
然后我将手机中的图像通过电子邮件发送给自己,血腥的东西显示得很好,这使我更加困惑,我唯一能想到的是图像太大而无法显示?
/////////////////////////////////////////////// /////////////////////////////////////////////
我正在尝试创建一个图像,然后将其保存到文件中,它在屏幕上显示正常并创建文件,但是文件无法打开并且大小为0字节。以前有人有这个问题吗?这是我的代码:
private class SampleView extends View {
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
EANMaker e = new EANMaker("123456789963");
Paint paint = new Paint();
View view = this;
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(view.DRAWING_CACHE_QUALITY_HIGH);
canvas.drawColor(Color.WHITE);
Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(b);
c.drawRect(0, 0, 500, 500, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
paint.setTextSize(40);
paint.setTextScaleX(1.f);
paint.setAlpha(0);
paint.setAntiAlias(true);
c.drawText("Carton ID", 60, 40, paint);
paint.setTextSize(160);
Typeface tf = gettypeFace(MyActivity.this, "ean.ttf");
paint.setTypeface(tf);
c.drawText(e.getCode(), 60, 180, paint);
paint.setColor(Color.WHITE);
canvas.drawBitmap(b, 50,50, paint);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/barcode.png");
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
fos = null;
} catch (IOException ee) {
ee.printStackTrace();
} catch (Exception p)
{
p.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}
}
}
public Typeface gettypeFace(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e("TAG", "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
编辑,我一直在玩这个,现在可以得到一个6k的文件,但是这仍然无法打开,我怀疑其中一个画布没有被吸引到位图但是我完全失去了什么否则,我的最新代码如下
private class SampleView extends View {
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
EANMaker e = new EANMaker("123456789963");
Paint paint = new Paint();
canvas.drawColor(Color.WHITE);
Bitmap b = Bitmap.createBitmap(900, 480, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
canvas.drawBitmap(b,0,0, paint);
//paint.setTextScaleX(1.f);
paint.setColor(Color.BLACK);
paint.setTextSize(40);
c.drawText("Carton LABEL", 20, 40, paint);
c.drawText("Carton ID = 1 Packed By = Test User", 20, 80, paint);
c.drawText("Despatch Date = 05/08/2014", 20, 120, paint);
paint.setTextSize(160);
Typeface tf = gettypeFace(MyActivity.this, "ean.ttf");
paint.setTypeface(tf);
c.drawText(e.getCode(), 60, 280, paint);
paint.setColor(Color.WHITE);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/barcode.png");
b.compress(Bitmap.CompressFormat.PNG, 80, fos);
fos.flush();
fos.close();
fos = null;
} catch (IOException ee) {
ee.printStackTrace();
} catch (Exception p)
{
p.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}
}
}
public Typeface gettypeFace(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e("TAG", "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
和我的logcat证明没有错误:
08-05 13:13:02.371 26627-26627/com.example.imagetest I/System.out﹕ debugger has settled (1443)
08-05 13:13:03.082 26627-26627/com.example.imagetest D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
08-05 13:13:03.082 26627-26627/com.example.imagetest D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
08-05 13:13:03.092 26627-26627/com.example.imagetest D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
08-05 13:13:03.112 26627-26627/com.example.imagetest D/OpenGLRenderer﹕ Enabling debug mode 0
08-05 13:13:03.162 26627-26627/com.example.imagetest I/System.out﹕ Full code: 1234567899633
08-05 13:13:03.162 26627-26627/com.example.imagetest I/System.out﹕ Generated code: $!23E5GH-ijjgdd!
08-05 13:13:03.192 26627-26627/com.example.imagetest D/dalvikvm﹕ GC_FOR_ALLOC freed 84K, 13% free 9464K/10823K, paused 22ms, total 32ms
08-05 13:13:03.192 26627-26627/com.example.imagetest I/dalvikvm-heap﹕ Grow heap (frag case) to 12.093MB for 1728016-byte allocation
08-05 13:13:03.222 26627-26629/com.example.imagetest D/dalvikvm﹕ GC_CONCURRENT freed 39K, 12% free 11112K/12551K, paused 13ms+3ms, total 31ms
08-05 13:13:03.222 26627-26627/com.example.imagetest D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 9ms
答案 0 :(得分:1)
正在创建图像,它们太大而无法在设备上正确打开。