2016-04-05 13 views
0

これは私のコードの簡略化されたバージョンです。 appスクリーンの2つのスクリーンショットを取得し、first.pngとsec.pngのstoreはapp \ srcフォルダにあります。私は画像を開いて、それらが正しいスクリーンショットであることを見ることができます。しかし、私がbitmapfactoryを使用してそれらをビットマップに取得しようとすると、nullが返されます。私は絶対パスと相対パスを与えようとしました。私は他の同じ質問応答に従っていますので、重複としてマークする前に見てください。あなたはBitmapFactory.decodeFileNPEを取得している場合はBitmapFactoryのdecodeFileがnullを返します

class Test{ 
private static boolean compareImages() throws FileNotFoundException { 
    ​Bitmap bitmap1 = BitmapFactory.decodeFile("src/"+imgFirst); 
    Bitmap bitmap2 = BitmapFactory.decodeFile("src/"+imgSecond); 
    if (bitmap1.getWidth() != bitmap2.getWidth() || 
      bitmap1.getHeight() != bitmap2.getHeight()) { 
     return false; 
    } 

    for (int y = 0; y < bitmap1.getHeight(); y++) { 
     for (int x = 0; x < bitmap1.getWidth(); x++) { 
      if (bitmap1.getPixel(x, y) != bitmap2.getPixel(x, y)) { 
       return false; 
      } 
     } 
    } 
    return true; 
} 
public static boolean test() { 

    File scrFile = (Setup.getDriver()).getScreenshotAs(OutputType.FILE);//The Setup.getDriver() returns an AppiumDriver. 
    FileUtils.copyFile(scrFile, new File(imgFirst)); 
    scrFile = (Setup.getDriver()).getScreenshotAs(OutputType.FILE); 
    FileUtils.copyFile(scrFile, new File(imgSecond)); 
    if(compareImages()) 
     return false; 
    else 
     return true; 
} 
    private static String imgFirst = "first.png"; 
    private static String imgSecond = "sec.png"; 
} 
+0

' BitmapFactory'をインポートしましたか? – nullpointer

+0

'android.graphics.Bitmap'と ' android.graphics.BitmapFactory' – Gsquare

+0

大丈夫です。例外のスタックトレースを追加できますか? – nullpointer

答えて

0

あなたはあなたのファイルの絶対パスを取得しようとすると、それを使用する必要があります:参照

String firstImage=new File(getFilesDir(), imgFirst).getAbsolutePath(); 
​Bitmap bitmap1 = BitmapFactory.decodeFile(firstImage); 

:ここで、あなたのコード内でSO-3388898

関連する問題