0

StackOverflow コミュニティの皆様、こんにちは。

私のアプリケーションでは、ユーザーが現在のビューをキャプチャできるボタンが必要です。

そのため、次のメソッドを作成しました (方向: Android save view to jpg or png ):

private LinearLayout container; 

public void createImageOfCurrentView(View v) {
    if (isExternalStoragePresent()) {
        container = (LinearLayout) findViewById(R.id.container);
        container.setDrawingCacheEnabled(true);
        Bitmap b = container.getDrawingCache();

        File dir = new File(Environment.getExternalStorageDirectory().getPath()
          + "/" + getPackageName() + "/");
        dir.mkdirs();

        File file = new File(dir, "image.jpg");
        FileOutputStream fos;

        try {
            fos = new FileOutputStream(file);
            b.compress(CompressFormat.JPEG, 95, fos); // NullPointerException!
            Toast.makeText(this, "The current view has been succesfully saved "
              + "as image.", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "Unfortunatly an error occured and this "
              + "action failed.", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(this, "Bacause of lacking access to the storage "
          + "of this device, the current view couldn't be saved as an image.",
          Toast.LENGTH_LONG).show();
    }
}

問題は、 を作成しようとしLogCatたときに が発生したことによるものです。したがって、おそらくその方法は機能していません。電話でファイルが生成されます。ただし、サイズは 0 バイトで、画像は表示されません。希望どおりに機能させるために何をしなければならないかについての提案をいただければ幸いです。NullPointerExceptionjpegcontainer.getDrawingCache()

4

2 に答える 2

0

それを試してみてください -

public static Bitmap convertViewToBitmap(View view) {
   Bitmap result = null;

   try {
      result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
      view.draw(new Canvas(result));
   }catch(Exception e) {}    
   return result;
}
于 2012-04-16T08:58:55.703 に答える