我正在尝试合并2张图片,因此我在这里搜索并看到了代码片段combining two png files in android

Bitmap bottomImage = new BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = new BitmapFactor.decodeFile("myOtherPNG.png");


Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);

// bottomImage is now a composite of the two.

// To write the file out to the SDCard:
OutputStream os = null;
try {
    os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
    image.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
    e.printStackTrace();
}


我尝试使用它,但是由于某些原因Bitmap bottomImage = new BitmapFactory.decodeFile(“ myFirstPNG.png”);给出一个错误,BitmapFactory无法重新转换为一种类型,但我已经拥有了两者

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


我正在使用phonegap并将其放在主要活动中进行测试

最佳答案

decodeFile是一个静态方法,需要在类范围内按以下方式调用:

Bitmap bottomImage = BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = BitmapFactor.decodeFile("myOtherPNG.png");


(注意,已删除new关键字)

07-24 09:29